second to the last field 
Author Message
 second to the last field

I need to grab the second to the last field in a string and assign it to a
variable in a script.

The string is generated from a command whose output looks like this:

 FileName                   Service                  Host                
PID
 ------------------------    ----------------          ----------------  
----------
/dev/uslxp098_s1_pi    uslxp098_s1            uslxp098           4290
/dev/uslxp098_p1_pi    Port Number 2         pserv5             18150

I know the FileName. I can get the PID with something like this:

myPID=`command | grep $myFileName | awk '{print $NR}'`

How would I get the Host when the number of fields can vary?

Thanks for any help!
--
John



Sun, 29 Apr 2001 03:00:00 GMT  
 second to the last field
There are several ways you could get the desired effect.

1.Using any file as an input file, you can always access the last field by
$(NF-1)

2. For this particular file (since it is probably generated in fixed field
lengths) you can set the FIELDWIDTHS internal variable to the field width of
each field so that the default FS character is ignored. Do not set FS in
your program because this will negate the effect of setting FIELDWIDTHS.
Setting FIELDWIDTHS is done like so (usually in the BEGIN block):
FIELDWIDTHS="23 23 19 10"

3. You may also use a simple cut command if you're not interested in doing
any special processing on the fields i.e. simply printing out a subset of
your input file

cut -c46-64 infile >outfile

Cesar

--
Please remove the UPPERCASE characters from my e-mail address for the real
thing


Quote:
>I need to grab the second to the last field in a string and assign it to a
>variable in a script.

>The string is generated from a command whose output looks like this:

> FileName                   Service                  Host
>PID
> ------------------------    ----------------          ----------------
>----------
>/dev/uslxp098_s1_pi    uslxp098_s1            uslxp098           4290
>/dev/uslxp098_p1_pi    Port Number 2         pserv5             18150

>I know the FileName. I can get the PID with something like this:

>myPID=`command | grep $myFileName | awk '{print $NR}'`

>How would I get the Host when the number of fields can vary?

>Thanks for any help!
>--
>John



Sun, 29 Apr 2001 03:00:00 GMT  
 second to the last field


Quote:
> ... you can set the FIELDWIDTHS internal variable to the field width of
> each field so that the default FS character is ignored...

This variable is an extention to POSIX-compliant awk, so it may not be
generally available. It is available in gawk.


Sun, 29 Apr 2001 03:00:00 GMT  
 second to the last field

Quote:

> I need to grab the second to the last field in a string and assign it to a
> variable in a script.

> The string is generated from a command whose output looks like this:

> FileName                   Service            Host               PID
> ------------------------   ----------------   ----------------   ----------
> /dev/uslxp098_s1_pi        uslxp098_s1        uslxp098           4290
> /dev/uslxp098_p1_pi        Port Number 2      pserv5             18150

> I know the FileName. I can get the PID with something like this:

> myPID=`command | grep $myFileName | awk '{print $NR}'`
>                                                 ^^^ [This should be $NF.]

> How would I get the Host when the number of fields can vary?

The value of the last field is in the built-in variable $NF, the second
to the last field is in $(NF - 1), etc. So the Host value will be in
$(NF - 1) provided that there is consistently a Host and a PID value on
each line and that they are consistently each one "word" (i.e., a string
of non-whitespace characters).

Your use of grep here is fraught with peril. What happens if the value
of $myFileName contains regular expression metacharacters? What happens
if the value of $myFileName happens to match a substring elsewhere on
the line? I can only guess what the value of $myFileName might be: maybe
you've already quoted all possible metacharacters in it and added a
beginning-of-line anchor (^) to it to ensure that it matches _only_
file names. I'm just suggesting you need to be very careful when using
arbitrary strings as regular expressions. Using fgrep ("fixed" grep,
also invoked by grep -F) would solve the first problem, but not the
second.

It may be better to use string comparison than regular expression
pattern matching in this case. Something like this:

myFileName='/dev/uslxp098_s1_pi'
myHost=`command | awk '$1 == mfn { print $(NF - 1); exit }' mfn=$myFileName`

You definitely should _not_ be processing the output of "command"
multiple times to pick out the different values on the line (Host, PID,
etc.).

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Sun, 29 Apr 2001 03:00:00 GMT  
 second to the last field

Quote:
> I need to grab the second to the last field in a string and assign it to a
> variable in a script.

>  FileName          Service          Host        PID

> /dev/uslxp098_s1_pi    uslxp098_s1            uslxp098           4290
> /dev/uslxp098_p1_pi    Port Number 2         pserv5             18150

> I know the FileName. I can get the PID with something like this:

> myPID=`command | grep $myFileName | awk '{print $NR}'`

NR == number of record == usually number of lines
NF == number of fields

The  above  --used alone--  uses one grep too many:
myPID=`command | awk '/'$myFileName'/ {print $NF}'`
myHOST=`command | awk '/'$myFileName'/ {print $(NF-1)}'`

Depending how long the execution of "command" is,
the follwing may be useful:
myLINE=`command | grep $myFileName`
myPID=`echo $myLINE  |  awk '{print $NF}'`
myHOST=`echo $myLINE  |  awk '{print $(NF-1)}'`

not tested
 LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Mon, 30 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. print last field in a record, 2nd last.

2. First Second Third Last Default

3. Problem reading data from a file (encounter EOF after second last record)

4. Strange Output if Getting last Field or 2nd to the last field.

5. Problems with hot fields on a second browse

6. Hot fields slow to display on second browse

7. Removing first ans last field

8. last field of array/case insensitive pattern matching query

9. how do i remove the last field with awk, and save it

10. Get Last Characters in a field?

11. print the last field on a line

12. Can AWK get the last field?

 

 
Powered by phpBB® Forum Software