Awk

From Noah.org
Revision as of 18:04, 17 April 2007 by Root (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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