- •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
Chapter 3: Reading Input Files |
33 |
}'
The close function is called to ensure that if two identical `@execute' lines appear in the input, the command is run for each one. See Section 3.8 [Closing Input Files and Pipes], page 33.
Given the input:
foo bar baz
@execute who bletch
the program might produce:
foo bar baz
hack |
ttyv0 |
Jul 13 14:22 |
|
|
hack |
ttyp0 |
Jul 13 |
14:23 |
(gnu:0) |
hack |
ttyp1 |
Jul 13 |
14:23 |
(gnu:0) |
hack |
ttyp2 |
Jul 13 |
14:23 |
(gnu:0) |
hack |
ttyp3 |
Jul 13 |
14:23 |
(gnu:0) |
bletch |
|
|
|
|
Notice that this program ran the command who and printed the result. (If you try this program yourself, you will get di erent results, showing you who is logged in on your system.)
This variation of getline splits the record into elds, sets the value of NF and recomputes the value of $0. The values of NR and FNR are not changed.
command | getline var
The output of the command command is sent through a pipe to getline and into the variable var. For example, the following program reads the current date and time into the variable current_time, using the date utility, and then prints it.
awk 'BEGIN {
"date" | getline current_time close("date")
print "Report printed on " current_time
}'
In this version of getline, none of the built-in variables are changed, and the record is not split into elds.
3.8 Closing Input Files and Pipes
If the same le name or the same shell command is used with getline more than once during the execution of an awk program, the le is opened (or the command is executed) only the rst time. At that time, the rst record of input is read from that le or command. The next time the same le or command is used in getline, another record is read from it, and so on.
This implies that if you want to start reading the same le again from the beginning, or if you want to rerun a shell command (rather than reading more output from the command), you must take special steps. What you must do is use the close function, as follows:
34 |
The AWK Manual |
close( lename)
or
close(command)
The argument lename or command can be any expression. Its value must exactly equal the string that was used to open the le or start the command|for example, if you open a pipe with this:
"sort -r names" | getline foo
then you must close it with this:
close("sort -r names")
Once this function call is executed, the next getline from that le or command will reopen thele or rerun the command.
close returns a value of zero if the close succeeded. Otherwise, the value will be non-zero.