Subject: Re: bug#22886: Request to get First column of first row

From: Bob Proulx <[email-masked]>
Date: Wed, 2 Mar 2016 18:16:22 -0700

tag 22886 + notabug
close 22886
thanks

Your email to the bug reporting address opens a bug ticket
automatically.  Since you are not reporting a bug but are simply
asking for scripting help I will go ahead and close the ticket.

Kannan E1 wrote:
> We are facing issue to get output of first column of first row.

To get the first row you could use head.

  head -n1

To get the first column you could use awk.

  awk '{print$1}'

To do both you could do this:

  ... | head -n1 | awk '{print$1}'

> hscroot@vahmc032:~> viosvrcmd -m Server-XXXXXXX -p va10XXX-VIOS2 -c "lsmap -all -npiv" | grep -i va10XXX

Using your example you could do this to get the first column of the
first row.

  viosvrcmd -m Server-XXXXXXX -p va10XXX-VIOS2 -c "lsmap -all -npiv" | head -n1 | awk '{print$1}'

Awk is powerful enough to do both of those and much more.  NR is the
Number of Records.  If the NR (Number of Records) is equal to 1 then
that is the first line.  The first line being the first row testing
for NR==1 is a pattern match that matches the first line only.  Then a
small optimization is to exit immediately so as not to read any of the
rest of the input.

  viosvrcmd -m Server-XXXXXXX -p va10XXX-VIOS2 -c "lsmap -all -npiv" | awk 'NR==1{print$1;exit}'

> Please guide us to get only vfchostX column alone.Thanks...
> 
> hscroot@vahmc032:~> viosvrcmd -m Server-XXXXXXX -p va10XXX-VIOS2 -c "lsmap 
> -all -npiv" | grep -i va10XXX
> vfchost46     U9117.MMC.10E45C6-V2-C180              15 va10XXXXX  AIX
> 
> Note:  awk '{print $1}' is not working.

These types of shell scripting questions are probably better sent to
the [email-masked] discussion mailing list.  Email there won't
open a bug ticket.  That is generally the best place for random
scripting questions using the utilities.  Since this ticket has
already been started please continue the conversation here for this
discussion.  Please keep [email-masked] in the recipient list
for this topic.

Bob