Matt Fiddles

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

User Tools

Site Tools


Sidebar

"Oh no, not again."

- A bowl of petunias on it's way to certain death.



Where will you go today?

Princess Leia, getting her first look at the Millennium Falcon:

"You came in that thing? You're braver than I thought!"
computers:linux-commands:awk

AWK

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

Others

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

http://www.unixcl.com/2009/01/round-functionality-with-awk-bash-shell.html

How To Use awk In Bash Scripting

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