“Even after decades of using Unix on thousands of systems, I
find that it’s still fun to discover various convolutions of sed
and awk commands to perform command line wizardry. There’s a lot
more to each of these tools than those uses I make of these
commands on a routine basis. Let’s take a look at some one-liners
you might not yet have tried.“One of my long-standing “tricks” for awk is using $NF to print
the last field on every line. Since NF represents the number of
fields on a line (e.g., 6), $NF represents the value of that last
field (e.g., $6). Printing the last field of every line, therefore,
might look like this:$ awk ‘{print $NF}’ myfile
“or this:
$ awk -F: ‘{print $NF}’ /etc/passwd
“depending on whether the file is white space delimited or
not.”
Unix Tip: Sed & Awk — Still friendly after all these years
By
Sandra Henry-Stocker
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends, & analysis