[olug] Unix Tip: EXTRACT THAT LAST FIELD

Unix Guru Universe listserv at ugu.com
Sun Nov 17 19:00:53 UTC 2002


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

			      UNIX GURU UNIVERSE 
			         UNIX HOT TIP

			Unix Tip 2147 - November 17, 2002

		    http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


EXTRACT THAT LAST FIELD

You can use 'cut' to extract the last field of a line if you know
how many fields there are, eg:

field=`cut -d: -f8 file`

But if you don't know the maximum number of fields or the number
of fields per line are not consistent, awk can come to the rescue.
awk has the inbuilt variable NF for the number of fields.  By using
this variable we can use it to extract the last field by using:

field=`awk -F: '{print $NF}'`

or you can use calculations to retrieve any field relative to the last field.  
For example to retrieve the second last field, use:

field=`awk -F: '{print $(NF-1)}'`


This tip generously supported by: peters at ginini.com.au




--------------------------------------------------------------------------
To Subscribe:    http://www.ugu.com/sui/ugu/show?tip.subscribe
To Unsubscribe:  http://www.ugu.com/sui/ugu/show?tip.unsubscribe
To Submit A Tip: http://www.ugu.com/sui/ugu/show?tip.today

==========================================================================
DISCLAIMER: All UNIX HOT TIPS ARE OWNED BY THE UNIX GURU UNIVERSE AND ARE
NOT TO BE SOLD, PRINTED OR USED WITHOUT THE WRITTEN CONSENT OF THE UNIX 
GURU UNIVERSE. ALL TIPS ARE "USE AT YOUR OWN RISK". UGU  ADVISES THAT 
ALL TIPS BE TESTED IN A NON-PRODUCTION DEVELOPMENT ENVIRONMENT FIRST.

Unix Guru Universe - www.ugu.com - tips at ugu.com - Copyright 1994-2001
==========================================================================




More information about the OLUG mailing list