Total Pageviews

Thursday, March 15, 2012

awk KB

http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/

This program prints the maximum number of fields on any input line.

awk 'NF > 0' data
awk '{ if (NF > max) max = NF } END { print max }'
       
# Print first two fields in opposite order :

awk '{ print $2, $1 }' file

# Print length of string in 2nd column **********

awk '{print length($2)}' file

# Print lines longer than 72 characters : ******

awk 'length > 72' file

# Print fields in reverse order :

awk '{ for (i = NF; i > 0; --i) print $i }' file

# Add up first column, print sum and average : *****

awk '{ s += $1 } END { print "sum is", s, " average is", s/NR }' file

# Print all lines whose first field is different from previous one :

awk '$1 != prev { print; prev = $1 }' file

# Print column 3 if column 1 > column 2 :

awk '$1 > $2 {print $3}' file

# Print line if column 3 > column 2 :

awk '$3 > $2' file

# Print every line after erasing the 2nd field

awk '{$2 = ""; print}' file

No comments:

Post a Comment