Mohammedz.com

For Linux and Shell scripting.

How to print last field from a line using awk & field separator?.

1 Comment


How to print last field from a line using awk & fiel separator?.

awk provides NF for the number of fields. So it will give you the integer for the number of fields on a line.

e.g.:
awk -F/ ‘{print $NF}’ <filename>

This will print last field (considering / as field separator) from file ‘filename’. This is useful if you want to extract only the filename from its path.

Advertisements

One thought on “How to print last field from a line using awk & field separator?.

  1. Thanks for that!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s