Matt Fiddles

Life's so vast, there's just so much to do...

User Tools

Site Tools


Han Solo: "This is not going to work."

Luke Skywalker: "Why didn't you say so before?"

Han Solo: "I did say so before!"

Where will you go today?

"Concentrate...feel the Force flow. Yes. Good. Calm, yes. Through the Force, things you will see. Other places. The future...the past. Old friends long gone."

- Yoda


NF is for the number of fields in the current record.

NR is for the number of records in the input file.

This will print the current record and checks for the field if it is not equals to 0

awk '{print ; if (NF != 0) print $0}' 

Print the last line

awk 'END {print $0}'

Print the number of lines

awk 'END {print NR}'

Print second to last column

awk '{print $(NF-1)}'

Famous Awk One-Liners Explained, Part III: Selective Printing and Deleting of Certain Lines


Round numbers

... | awk '{ print $2/40.95 }' OFMT="%3.1f"
... | awk '{ printf( "%3.1f\n", $2/40.95 )}'
echo "4.5" | awk '{printf("%d\n",$0+=$0<0?-0.5:0.5)}'
echo "23.54" | awk '{printf("%d\n",$1 + 0.5)}'  # prints 24
echo "23.49" | awk '{printf("%d\n",$1 + 0.5)}'  * prints 23

How To Use awk In Bash Scripting

computers/linux-commands/awk.txt · Last modified: Apr 5, 2015 (4 years ago) by Matt Bagley