- •Preface
- •History of awk
- •GNU GENERAL PUBLIC LICENSE
- •Preamble
- •TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
- •How to Apply These Terms to Your New Programs
- •Using this Manual
- •Data Files for the Examples
- •Getting Started with awk
- •A Very Simple Example
- •An Example with Two Rules
- •A More Complex Example
- •How to Run awk Programs
- •One-shot Throw-away awk Programs
- •Running awk without Input Files
- •Running Long Programs
- •Executable awk Programs
- •Comments in awk Programs
- •awk Statements versus Lines
- •When to Use awk
- •Reading Input Files
- •How Input is Split into Records
- •Examining Fields
- •Non-constant Field Numbers
- •Changing the Contents of a Field
- •Specifying how Fields are Separated
- •Multiple-Line Records
- •Explicit Input with getline
- •Closing Input Files and Pipes
- •Printing Output
- •The print Statement
- •Examples of print Statements
- •Output Separators
- •Controlling Numeric Output with print
- •Using printf Statements for Fancier Printing
- •Introduction to the printf Statement
- •Format-Control Letters
- •Examples of Using printf
- •Redirecting Output of print and printf
- •Redirecting Output to Files and Pipes
- •Closing Output Files and Pipes
- •Standard I/O Streams
- •Patterns
- •Kinds of Patterns
- •Regular Expressions as Patterns
- •How to Use Regular Expressions
- •Regular Expression Operators
- •Case-sensitivity in Matching
- •Comparison Expressions as Patterns
- •Boolean Operators and Patterns
- •Expressions as Patterns
- •Specifying Record Ranges with Patterns
- •BEGIN and END Special Patterns
- •The Empty Pattern
- •Overview of Actions
- •Expressions as Action Statements
- •Constant Expressions
- •Variables
- •Assigning Variables on the Command Line
- •Arithmetic Operators
- •String Concatenation
- •Comparison Expressions
- •Boolean Expressions
- •Assignment Expressions
- •Increment Operators
- •Conversion of Strings and Numbers
- •Numeric and String Values
- •Conditional Expressions
- •Function Calls
- •Operator Precedence (How Operators Nest)
- •Control Statements in Actions
- •The if Statement
- •The while Statement
- •The do-while Statement
- •The for Statement
- •The break Statement
- •The continue Statement
- •The next Statement
- •The exit Statement
- •Arrays in awk
- •Introduction to Arrays
- •Referring to an Array Element
- •Assigning Array Elements
- •Basic Example of an Array
- •Scanning all Elements of an Array
- •The delete Statement
- •Using Numbers to Subscript Arrays
- •Multi-dimensional Arrays
- •Scanning Multi-dimensional Arrays
- •Built-in Functions
- •Calling Built-in Functions
- •Numeric Built-in Functions
- •Built-in Functions for String Manipulation
- •Built-in Functions for Input/Output
- •The return Statement
- •Built-in Variables
- •Built-in Variables that Control awk
- •Built-in Variables that Convey Information
- •Invoking awk
- •Command Line Options
- •Other Command Line Arguments
- •Index
Index |
125 |
Index
#
`#' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 `#!' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
$
$ ( eld operator). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
-
`-f' option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 -f option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 `-F' option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 -F option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 `-v' option . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
A
accessing elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 acronym . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 action, curly braces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 action, default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 action, de nition of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 action, separating statements. . . . . . . . . . . . . . . . . . . . . . 55 addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 and operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 applications of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 ARGC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 arguments in function call . . . . . . . . . . . . . . . . . . . . . . . . . 70 arguments, command line . . . . . . . . . . . . . . . . . . . . . . . . 105
ARGV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102, 105 arithmetic operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 array assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 array reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 arrays, de nition of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 arrays, deleting an element . . . . . . . . . . . . . . . . . . . . . . . . 85 arrays, multi-dimensional subscripts . . . . . . . . . . . . . . . 86 arrays, presence of elements . . . . . . . . . . . . . . . . . . . . . . . 82 arrays, special for statement . . . . . . . . . . . . . . . . . . . . . . 84 assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 assignment to elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 associative arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 awk language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 awk program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
B
backslash continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 basic function of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 `BBS-list' le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 BEGIN special pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 body of a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
boolean expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 boolean operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 boolean patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 bu ering output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 bu ers, ushing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 built-in functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 built-in variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 built-in variables, user modi able . . . . . . . . . . . . . . . . 101
C
call by reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 call by value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 calling a function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 case sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 changing contents of a eld. . . . . . . . . . . . . . . . . . . . . . . . 24 changing the record separator . . . . . . . . . . . . . . . . . . . . . 21 close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 43 closing input les and pipes . . . . . . . . . . . . . . . . . . . . . . . 33 closing output les and pipes . . . . . . . . . . . . . . . . . . . . . . 43 command line. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 command line formats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 command line, setting FS on. . . . . . . . . . . . . . . . . . . . . . . 26 comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 comparison expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 comparison expressions as patterns . . . . . . . . . . . . . . . . 51 computed regular expressions. . . . . . . . . . . . . . . . . . . . . . 48 concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 conditional expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 constants, types of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 continuation of lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 continue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 control statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 conversion of strings and numbers. . . . . . . . . . . . . 67, 68 conversions, during subscripting . . . . . . . . . . . . . . . . . . . 86
CONVFMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62, 67, 86, 101 curly braces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
D
default action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 default pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 de ning functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 delete statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 deleting elements of arrays . . . . . . . . . . . . . . . . . . . . . . . . 85 di erences between gawk and awk . . . . . . . . . . . . . 57, 61 di erences: gawk and awk. . . . . . . . . . . . . . . . . . . . . . . . . . 44 division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 documenting awk programs . . . . . . . . . . . . . . . . . . . . . . . . 18 dynamic regular expressions . . . . . . . . . . . . . . . . . . . . . . . 48
126
E
element assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 element of array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 empty pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 END special pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 ENVIRON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 escape sequence notation . . . . . . . . . . . . . . . . . . . . . . . . . . 57 examining elds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 executable scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 exit statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 explicit input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 expression, conditional . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 expressions, assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 expressions, boolean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 expressions, comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The AWK Manual
input redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 input, explicit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 input, getline command. . . . . . . . . . . . . . . . . . . . . . . . . . 30 input, multiple line records . . . . . . . . . . . . . . . . . . . . . . . . 29 input, standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 interaction, awk and other programs . . . . . . . . . . . . . . . 93 `inventory-shipped' le . . . . . . . . . . . . . . . . . . . . . . . . . . 12 invocation of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
L
language, awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 length. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 logical operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 long options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 loops, exiting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 lvalue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
F
eld separator, choice of. . . . . . . . . . . . . . . . . . . . . . . . . . . 26eld separator, FS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25eld separator: on command line . . . . . . . . . . . . . . . . . . 26eld, changing contents of . . . . . . . . . . . . . . . . . . . . . . . . . 24elds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22elds, separating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25le descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44le, awk program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 FILENAME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21, 102ushing bu ers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 FNR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 103 for (x in : : :) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 for statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 format speci er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 format string. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 formatted output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 FS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 101 function call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 function de nition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 functions, user-de ned . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
G
getline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 gsub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
H
history of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 how awk works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
I
if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 increment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 input le, sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
M
manual, using this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90, 91 metacharacters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 modi ers (in format speci ers) . . . . . . . . . . . . . . . . . . . . 39 multi-dimensional subscripts . . . . . . . . . . . . . . . . . . . . . . 86 multiple line records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 multiple passes over data. . . . . . . . . . . . . . . . . . . . . . . . . 106 multiple statements on one line. . . . . . . . . . . . . . . . . . . . 20 multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
N
next statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 NF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 103 not operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 NR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 103 number of elds, NF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 number of records, NR or FNR . . . . . . . . . . . . . . . . . . . . . . 22 numbers, used as subscripts . . . . . . . . . . . . . . . . . . . . . . . 86 numeric constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 numeric value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
O
OFMT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 68, 101 OFS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 101 one-liners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 operators, $ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 operators, arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 operators, assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 operators, boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 operators, increment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 operators, regexp matching. . . . . . . . . . . . . . . . . . . . . . . . 47 operators, relational . . . . . . . . . . . . . . . . . . . . . . . . . . 51, 62 operators, string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Index |
127 |
operators, string-matching . . . . . . . . . . . . . . . . . . . . . . . . 47 options, command line . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 options, long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 or operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 ORS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37, 101 output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 output eld separator, OFS . . . . . . . . . . . . . . . . . . . . . . . . 37 output record separator, ORS . . . . . . . . . . . . . . . . . . . . . . 37 output redirection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 output, bu ering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 output, formatted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 output, piping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
P
passes, multiple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 pattern, case sensitive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 pattern, comparison expressions . . . . . . . . . . . . . . . . . . . 51 pattern, default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 pattern, de nition of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 pattern, empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 pattern, regular expressions . . . . . . . . . . . . . . . . . . . . . . . 47 patterns, BEGIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 patterns, boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 patterns, END. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 patterns, range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 patterns, types of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 pipes for output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 `print $0' . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 print statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 printf statement, syntax of . . . . . . . . . . . . . . . . . . . . . . . 38 printf, format-control characters . . . . . . . . . . . . . . . . . 38 printf, modi ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 printing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 program le. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 program, awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 program, de nition of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 program, self contained . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 programs, documenting . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Q
quotient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
60 |
R
range pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 reading les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 reading les, getline command . . . . . . . . . . . . . . . . . . . 30 reading les, multiple line records . . . . . . . . . . . . . . . . . 29 record separator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 records, multiple line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 redirection of input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 redirection of output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 reference to array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
regexp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . . |
47 |
regexp as expression . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
63 |
regexp operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
62 |
regexp search operators . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
47 |
regular expression matching operators . . . . . . . . . . |
. . . |
47 |
regular expression metacharacters . . . . . . . . . . . . . . |
. . . |
48 |
regular expressions as eld separators . . . . . . . . . . |
. . . |
26 |
regular expressions as patterns . . . . . . . . . . . . . . . . . |
. . . |
47 |
regular expressions, computed . . . . . . . . . . . . . . . . . . |
. . . |
48 |
relational operators . . . . . . . . . . . . . . . . . . . . . . . . . . . |
51, |
62 |
remainder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
60 |
removing elements of arrays . . . . . . . . . . . . . . . . . . . . |
. . . |
85 |
return statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
98 |
RLENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
91, |
103 |
RS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
21, |
101 |
RSTART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
91, |
103 |
rule, de nition of. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
13 |
running awk programs . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
16 |
running long programs . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . |
17 |
S
sample input le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 scanning an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 script, de nition of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 scripts, executable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 scripts, shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 self contained programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 shell scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 side e ect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 single quotes, why needed . . . . . . . . . . . . . . . . . . . . . . . . . 16 split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 sprintf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 standard error output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 standard input . . . . . . . . . . . . . . . . . . . . . . . . . . . 16, 21, 44 standard output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 string constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 string operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 string-matching operators . . . . . . . . . . . . . . . . . . . . . . . . . 47 sub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 subscripts in arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 SUBSEP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86, 101 substr. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 system. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
T |
|
tolower . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
93 |
toupper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
93 |
U
use of comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 user-de ned functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 user-de ned variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
128
uses of awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 using this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
V
variables, user-de ned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The AWK Manual
W
what is awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 when to use awk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
i
Short Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . . . . . . . . . . . . . . . 3 1 Using this Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2 Getting Started with awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3 Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4 Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5 Useful \One-liners" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 6 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 7 Overview of Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8 Expressions as Action Statements . . . . . . . . . . . . . . . . . . . . . . . 57 9 Control Statements in Actions . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10 Arrays in awk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 12 User-de ned Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 13 Built-in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 14 Invoking awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Appendix A awk Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Appendix B Sample Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Appendix C Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
ii |
The AWK Manual |
iii
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
History of awk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
GNU GENERAL PUBLIC LICENSE . . . . . . . . . . . . . . 3
Preamble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND
MODIFICATION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 How to Apply These Terms to Your New Programs . . . . . . . . . . . . . . . . . . . 8
1 Using this Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1 Data Files for the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Getting Started with awk . . . . . . . . . . . . . . . . . . . . . . 13
2.1 A Very Simple Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2 An Example with Two Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3 A More Complex Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.4 How to Run awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.4.1 One-shot Throw-away awk Programs . . . . . . . . . . . . . . . . . . 16 2.4.2 Running awk without Input Files . . . . . . . . . . . . . . . . . . . . . . 16 2.4.3 Running Long Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4.4 Executable awk Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5 Comments in awk Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.6 awk Statements versus Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.7 When to Use awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3 Reading Input Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1 How Input is Split into Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.2 Examining Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.3 Non-constant Field Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.4 Changing the Contents of a Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.5 Specifying how Fields are Separated . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 3.6 Multiple-Line Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.7 Explicit Input with getline. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.8 Closing Input Files and Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4 Printing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.1 The print Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.2 Examples of print Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 4.3 Output Separators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.4 Controlling Numeric Output with print . . . . . . . . . . . . . . . . . . . . . . . 37 4.5 Using printf Statements for Fancier Printing . . . . . . . . . . . . . . . . . . 38
4.5.1 Introduction to the printf Statement . . . . . . . . . . . . . . . . . 38 4.5.2 Format-Control Letters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.5.3 Modi ers for printf Formats . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.5.4 Examples of Using printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.6 Redirecting Output of print and printf . . . . . . . . . . . . . . . . . . . . . . . 42 4.6.1 Redirecting Output to Files and Pipes . . . . . . . . . . . . . . . . . 42 4.6.2 Closing Output Files and Pipes . . . . . . . . . . . . . . . . . . . . . . . 43
4.7 Standard I/O Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
iv |
The AWK Manual |
5 Useful \One-liners" . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.1 Kinds of Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.2 Regular Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 6.2.1 How to Use Regular Expressions . . . . . . . . . . . . . . . . . . . . . . 47 6.2.2 Regular Expression Operators. . . . . . . . . . . . . . . . . . . . . . . . . 48 6.2.3 Case-sensitivity in Matching . . . . . . . . . . . . . . . . . . . . . . . . . . 50
6.3 Comparison Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.4 Boolean Operators and Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 6.5 Expressions as Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 6.6 Specifying Record Ranges with Patterns. . . . . . . . . . . . . . . . . . . . . . . . 53 6.7 BEGIN and END Special Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.8 The Empty Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7 Overview of Actions . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8 Expressions as Action Statements. . . . . . . . . . . . . . 57
8.1 Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.2.1 Assigning Variables on the Command Line . . . . . . . . . . . . . 60
8.3 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.4 String Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
8.5 Comparison Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
8.6 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.7 Assignment Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.8 Increment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.9 Conversion of Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
8.10 Numeric and String Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.11 Conditional Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
8.12 Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8.13 Operator Precedence (How Operators Nest) . . . . . . . . . . . . . . . . . . . 71
9 Control Statements in Actions . . . . . . . . . . . . . . . . . 73
9.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 9.2 The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.3 The do-while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 9.4 The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 9.5 The break Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 9.6 The continue Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 9.7 The next Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 9.8 The exit Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
10 Arrays in awk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
10.1 Introduction to Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 10.2 Referring to an Array Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 10.3 Assigning Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.4 Basic Example of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 10.5 Scanning all Elements of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 10.6 The delete Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 10.7 Using Numbers to Subscript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 10.8 Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 10.9 Scanning Multi-dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
v
11 Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.1 Calling Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 11.2 Numeric Built-in Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 11.3 Built-in Functions for String Manipulation . . . . . . . . . . . . . . . . . . . . 90 11.4 Built-in Functions for Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . 93
12 User-de ned Functions . . . . . . . . . . . . . . . . . . . . . . . 95
12.1 Syntax of Function De nitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 12.2 Function De nition Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 12.3 Calling User-de ned Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 12.4 The return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
13 Built-in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
13.1 Built-in Variables that Control awk . . . . . . . . . . . . . . . . . . . . . . . . . . 101 13.2 Built-in Variables that Convey Information . . . . . . . . . . . . . . . . . . . 102
14 Invoking awk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
14.1 Command Line Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 14.2 Other Command Line Arguments. . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Appendix A awk Summary . . . . . . . . . . . . . . . . . . . . . |
107 |
A.1 Command Line Options Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 A.2 Language Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 A.3 Variables and Fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 A.3.1 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 A.3.2 Built-in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 A.3.3 Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 A.3.4 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
A.4 Patterns and Actions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 A.4.1 Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 A.4.2 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 A.4.3 Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 A.4.3.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 A.4.3.2 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . 113 A.4.3.3 I/O Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 A.4.3.4 printf Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 114 A.4.3.5 Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . 115 A.4.3.6 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 A.4.3.7 String Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Appendix B Sample Program . . . . . . . . . . . . . . . . . . 119
Appendix C Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
vi |
The AWK Manual |