Bash Tricks: Print the Last Substring from a Delimited String

@~$ echo "abc:cde:efg" | awk 'BEGIN {FS=":"} {print $NF}'

The variable NF holds the number or records in the string, which is 3 in this case. So when you’re do a print $NF, you are actually executing print $3, and thus printing the last record/substring.

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

Leave a Reply