Matt Fiddles

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

User Tools

Site Tools


Sidebar

"Pages one and two [of Zaphod's presidential speech] had been salvaged by a Damogran Frond Crested Eagle and had already become incorporated into an extraordinary new form of nest which the eagle had invented. It was constructed largely of papier mache and it was virtually impossible for a newly hatched baby eagle to break out of it. The Damogran Frond Crested Eagle had heard of the notion of survival of the species but wanted no truck with it."

- An example of Damogran wildlife.



Where will you go today?

Darth Vader: "Impressive. Most impressive. Obi-wan has taught you well. You have controlled your fear. Now, release your anger! Only your hatred can destroy me!"
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 (3 years ago) by Matt Bagley