Awk

Here, awk has simply selected the lines that match carrot:

[email protected]:~ > awk '{print $1}' foods boiled fried grilled grated

In this example, awk has printed the first field of each line, as defined by {'print $1'}.Using $2 here gives us the second field, while $0 represents the whole line.

You can also define the separator to be something else. In the example that follows, the option -F\: specifies that the field separator is a colon, allowing you to select a particular field (the fifth, which is the user's real name) from /etc/passwd, which is a colon-separated file.

[email protected]:~ > awk -F\: '{print $5}' /etc/passwd root bin [ ... ]

Guest User awk has various useful built-in functions. For example:

[email protected]:~ > cat morefoods boiled carrots and fried bacon fried potatoes and grilled sausages and mushrooms grilled onions grated carrot [email protected]:~ > awk 'NF > 2' morefoods boiled carrots and fried bacon fried potatoes and grilled sausages and mushrooms

NF represents the number of fields; in this example, by using 'NF > 2' you have selected the lines with more than two fields. This could be useful, for example, if you are trying to solve a problem of importing structured data into an application where the import fails because of some badly formed lines having the wrong number of fields:

[email protected]:~ > awk 'NF > 2 {print $4}' morefoods fried grilled

So in the preceding example, you have printed the fourth field of each line, which has more than two fields.

[email protected]:~ > awk '{ print NF '':'' $0 }' morefoods 5:boiled carrots and fried bacon

7:fried potatoes and grilled sausages and mushrooms 2:grilled onions 2:grated carrot

Now in this example, you have printed the number of fields followed by a colon and the whole line (which is represented by $0).

An awk script can be run from the command line with a command such as awk -f scriptname file. For example, save the following as script.awk:

END{print NR} Then do this:

[email protected]:~ > awk -f script.awk morefoods boiled:carrots:5

fried:potatoes:7

grilled:onions:2

grated:carrot:2

The first two fields of each line of the file have been printed, with a colon between them, followed by another colon and the number of fields (NF) in the line. Then the END section has printed the value of NR (the number of records) after finishing looping through the file.

GNU awk has documentation on the system in the form of an info file; type info awk to view it. The latest version of the GNU awk manual is always available at www.gnu.org /software/gawk/manual/. You can find a number of books available on awk, including sed & awk by Dale Dougherty and Arnold Robbins (O'Reilly, 1997).

Was this article helpful?

0 0

Post a comment