Unix Tip #4594- October 9, 2023


SEARCH : Home : Help : Today's Tip

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@ginini.com.au



NOTE: All tips provided are USE AT YOUR OWN RISK. Tips are submitted by various unix admins around the globe. UGU suggest you read and test each tip in a non-volitile environment before placing into production.


LAST 5 TIPS
4593 - FULL OF FILESYSTEM INODES
4592 - FORGET THE CRONTAB MAN
4591 - POWER OF BACKQUOTES
4590 - PING THE HOST TABLE
4589 - A SHAMELESS PLUG


I want to SUBSCRIBE and get a UGU Tip everyday.
I want to UNSUBSCRIBE and NOT get a UGU Tip everyday.

If you have a UNIX TIP let us know, we just may use it:
(All tips become the property of the Unix Guru Universe)
Email Address:

Yes, email me a Hot Unix Tip everday.

Enter Hot Unix Tip (optional):

Yes, I will support this tip

Captcha (not case sensitive):


Please enter the above letters:


HOME | Flavors | Admin | Network | Security | S/W | Help | Events | Vendors | Careers | Internet
About | Add Link | Feedback | Search

Copyright © 1994-2005 Unix Guru Universe