Awk One Liners
I’ve been doing this stuff all day, so let me show you few nifty awk tricks. For these examples lets assume we have a tab delimited file with n columns and m rows.
To take an average of each row (average all the values from each column of a given row do the following:
awk '{sum=0; for(i=1; i<=NF; i++){sum+=$i}; sum/=NF; print sum}' file
The NF variable is a reserved awk word which expands to the number of fields (or columns) in the current row. If you want to take an average of all the columns do:
awk '{for(i=1; i<=NF; i++){sum[i]+=$i}} END {for(i=1; i<=NF; i++){printf sum[i]/NR "\t"}}' file
The NR variable is another awk built-in, that gives you number of records (or rows) read. You can exploit NR To add line numbers to your file do:
awk '{print NR, $0}' file
Of course the same can be accomplished by:
cat -n file
To find the number of lines in a file you can do:
awk 'END {print NR}' file
If you want to find the combined number of lines in all the files passed in on the command line do:
awk 'END {print FNR}' file1 file2 file3 ...
A lot of data manipulation can be done with some awk magic, and simple unix commands such as grep, paste, wc and etc..
Related Posts:


September 28th, 2008 at 1:58 am (10271) [Quote]
Hi. I wanted to let you know that I just wrote a blog post about Awk One-Liners.
In this post I explain all the famous (you’ll see what I mean) Awk one liners.
The post is here:
Famous Awk One-Liners Explained
Sincerely,
Posted usingPeteris