Classic Shell Scripting by Arnold Robbins and Nelson H. F. Beebe

Classic Shell Scripting by Arnold Robbins and Nelson H. F. Beebe

Author:Arnold Robbins and Nelson H. F. Beebe
Language: eng
Format: mobi, epub
Tags: COMPUTERS / Operating Systems / UNIX
Publisher: O’Reilly Media
Published: 2008-12-17T05:00:00+00:00


Actions

We have now covered most of the awk language elements needed to select records. The action section that optionally follows a pattern is, well, where the action is: it specifies how to process the record.

awk has several statement types that allow construction of arbitrary programs. However, we delay presentation of most of them until Section 9.7. For now, apart from the assignment statement, we consider only the simple print statement.

In its simplest form, a bare print means to print the current input record ($0) on standard output, followed by the value of the output record separator, ORS, which is by default a single newline character. These programs are therefore equivalent:

1 Pattern is true, default action is to print NR > 0 { print } Print when have records, is always true 1 { print } Pattern is true, explicit print, default value { print } No pattern is treated as true, explicit print, default value { print $0 } Same, but with explicit value to print

A one-line awk program that contained any of those lines would simply copy the input stream to standard output.

More generally, a print statement can contain zero or more comma-separated expressions. Each is evaluated, converted to a string if necessary, and output on standard output, separated by the value of the output field separator, OFS. The last item is followed by the value of the output record separator, ORS.

The argument lists for print and its companions printf and sprintf (see Section 9.9.8) may optionally be parenthesized. The parentheses eliminate a parsing ambiguity when the argument list contains a relational operator, since < and > are also used in I/O redirection, as described in Section 9.7.6 and Section 9.7.7.

Here are some complete awk program examples. In each, we print just the first three input fields, and by omitting the selection pattern, we select all records. Semicolons separate awk program statements, and we vary the action code slightly to change the output field separators:

$ echo 'one two three four' | awk '{ print $1, $2, $3 }' one two three $ echo 'one two three four' | awk '{ OFS = "..."; print $1, $2, $3 }' one...two...three $ echo 'one two three four' | awk '{ OFS = "\n"; print $1, $2, $3 }' one two three

Changing the output field separator without assigning any field does not alter $0:

$ echo 'one two three four' | awk '{ OFS = "\n"; print $0 }' one two three four

However, if we change the output field separator, and we assign at least one of the fields (even if we do not change its value), then we force reassembly of the record with the new field separator:

$ echo 'one two three four' | awk '{ OFS = "\n"; $1 = $1; print $0 }' one two three four



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.