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
Classic Shell Scripting by Arnold Robbins and Nelson H. F. Beebe.epub
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.
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Filmora Efficient Editing by Alexander Zacharias(5767)
The Infinite Retina by Robert Scoble Irena Cronin(5240)
Learn Wireshark - Fundamentals of Wireshark. by Lisa Bock(3965)
Linux Device Driver Development Cookbook by Rodolfo Giometti(3933)
Edit Like a Pro with iMovie by Regit(3415)
Linux Administration Best Practices by Scott Alan Miller(2858)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic & Jasmin Redzepagic(2835)
MCSA Windows Server 2016 Study Guide: Exam 70-740 by William Panek(2520)
Mastering PowerShell Scripting - Fourth Edition by Chris Dent(2390)
Docker on Windows by Stoneman Elton(2317)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2312)
Creative Projects for Rust Programmers by Carlo Milanesi(2235)
Hands-On AWS Penetration Testing with Kali Linux by Karl Gilbert(2107)
Hands-On Linux for Architects by Denis Salamanca(2051)
Programming in C (4th Edition) (Developer's Library) by Stephen G. Kochan(2004)
Computers For Seniors For Dummies by Nancy C. Muir(1996)
The Old New Thing by Raymond Chen(1940)
Linux Kernel Debugging by Kaiwan N Billimoria(1762)
