awk
Description
An awk program consists of one or more program lines containing a pattern and/or action in the hllowing format:
panern { action }
The pattern selects lines from the input file. The awk utility performs the action on all lines that the pattern selects. You must enclose the action within braces so that awk can differentiate it from the pattern . If a program line does not contain a pattern, awk selects all lines in the input file. If a program line does not contain an action, awk copies the selected lines to its standard output.
Patterns
You can use a regular expression (refer to Appendix A), enclosed within slashes, as a pattern. The ~ operator tests to see if a field or variable matches a regular expression-The !~operator tests for no match.You can process arithmetic and character relational expressions with the following relational operators.
Operator | Meaning |
<
<= == != >= > |
less than
less than or equal to equal to not equal to greater than or equal to greater than |
You can combine any of the patterns described above using the Boolean operators | | (OR) or && (AND).The comma is the range operator. If you separate two patterns with a comma on a single awk program line, awk selects a range of lines beginning with the first line that contains the first pattern. The last line awk selects is the next subsequent line that contains the second pattern. After awk finds the second pattern, it Starts the process over by looking for the first pattern again.Two unique patterns, BEGIN and END, allow you to execute commands before awk starts its processing and after it finishes. The awk utility executes the actions associated with the BEGIN pattern before, and with the END pattern after, it processes all the files in the file-list.
Actions
The action portion of an awk command causes awk to take action when it matches a pattern. If you do not specify an action, awk performs the default action, which is the Print command (explicitly represented as {print}). This action copies the record (normally a line-see “Variables” on the next page) from the input file to awk’s standard output.You can follow a Print command with arguments, causing awk to print just the arguments you specify. The arguments can be variables or string constants. Using awk, you can send the output from a Print command to a file(>), append it to a file (>>), or pipe it to the input of another program( | ). Unless you separate items in a Print command with commas, awk catenates them. Commas cause awk to separate the items with the output field separator (normally a [space]-see “Variables” on the next page). You can include several actions on one line within a set of braces by separating them with semicolons.