Difference between revisions of "Awk"
From Noah.org
Jump to navigationJump to searchLine 20: | Line 20: | ||
subversion-1.3.2 | subversion-1.3.2 | ||
subversion-1.3.2 | subversion-1.3.2 | ||
+ | |||
+ | Once you get much more complicated than this then you probably | ||
+ | want a real scripting language. Average of column 3: | ||
+ | awk '{total+=$3;count++;}END{print total/count;}' file |
Revision as of 14:18, 18 April 2007
About the most I ever use awk for is to print fields I want from a delimited file. For example, to print field #2 from a file (assuming white space between columns):
awk '{print $2;}' file
If the fields have other delimiters such as '/' then use the -F option to set the delimiter. For example to print the first directory from a list of file paths use this:
awk -F '/' '{print $1;}' file
So if I had a list of filenames such as:
subversion-1.3.2/README subversion-1.3.2/configure.in subversion-1.3.2/TRANSLATING subversion-1.3.2/subversion subversion-1.3.2/subversion/libsvn_fs_base subversion-1.3.2/subversion/libsvn_fs_base/bdb
Running that list through the example would produce
subversion-1.3.2 subversion-1.3.2 subversion-1.3.2 subversion-1.3.2 subversion-1.3.2 subversion-1.3.2
Once you get much more complicated than this then you probably want a real scripting language. Average of column 3:
awk '{total+=$3;count++;}END{print total/count;}' file