Linked by Thom Holwerda on Tue 27th May 2008 13:08 UTC, submitted by Ward D
General Development AWK is one of the most common UNIX tools to process text-based data in either files or datastreams. Written by Alfred Aho, Peter Weinberger, and Brian Kernighan, AWK "extensively uses the string datatype, associative arrays (that is, arrays indexed by key strings), and regular expressions." ComputerWorld interviewed Alfred Aho.
Thread beginning with comment 316366
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[4]: what an wonderful tool
by fernandotcl on Sat 31st May 2008 05:37 UTC in reply to "RE[3]: what an wonderful tool"
fernandotcl
Member since:
2007-08-12

Use the "-F" flag to specify the separator in awk.

In my experience, awk can always replace cut with ease, but the opposite isn't always true. Still, cut is lighter on resources and also more readable (the operation you're performing seems more explicit to me).

Reply Parent Score: 1

RE[5]: what an wonderful tool
by Doc Pain on Sat 31st May 2008 19:20 in reply to "RE[4]: what an wonderful tool"
Doc Pain Member since:
2006-10-08

Regarding the field separator:

Use the "-F" flag to specify the separator in awk.


That's correct. Another option is to set FS in your awk script. The solutions

awk -F ":" '{ print $2, $4; }' zeux.csv

or

awk 'BEGIN { FS = ":"; } { print $2, $4; }' zeux.csv

should give the same results. FS can be declared as a regular expression that makes "space or more spaces and / or tab or tabs" the field separator.

In my experience, awk can always replace cut with ease, but the opposite isn't always true. Still, cut is lighter on resources and also more readable (the operation you're performing seems more explicit to me).


Following the trend of "light weight solutions" and the philosophy to choose the best tool for each task, cut sometimes simply is the best solution. For example, in situations like

cat /etc/passwd | cut -d ":" -f 1

I wouldn't use awk.

Reply Parent Score: 2