- •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
16 |
The AWK Manual |
2.4 How to Run awk Programs
There are several ways to run an awk program. If the program is short, it is easiest to include it in the command that runs awk, like this:
awk 'program' inputle1 inputle2 : : :
where program consists of a series of patterns and actions, as described earlier.
When the program is long, it is usually more convenient to put it in a le and run it with a command like this:
awk -f programle inputle1 inputle2 : : :
2.4.1 One-shot Throw-away awk Programs
Once you are familiar with awk, you will often type simple programs at the moment you want to use them. Then you can write the program as the rst argument of the awk command, like this:
awk 'program' inputle1 inputle2 : : :
where program consists of a series of patterns and actions, as described earlier.
This command format instructs the shell to start awk and use the program to process records in the input le(s). There are single quotes around program so that the shell doesn't interpret any awk characters as special shell characters. They also cause the shell to treat all of program as a single argument for awk and allow program to be more than one line long.
This format is also useful for running short or medium-sized awk programs from shell scripts, because it avoids the need for a separate le for the awk program. A self-contained shell script is more reliable since there are no other les to misplace.
2.4.2 Running awk without Input Files
You can also run awk without any input les. If you type the command line:
awk 'program'
then awk applies the program to the standard input, which usually means whatever you type on the terminal. This continues until you indicate end-of- le by typing Control-d.
For example, if you execute this command:
awk '/th/'
whatever you type next is taken as data for that awk program. If you go on to type the following data:
Chapter 2: Getting Started with awk |
17 |
Kathy
Ben
Tom
Beth
Seth
Karen
Thomas
Control-d
then awk prints this output:
Kathy
Beth
Seth
as matching the pattern `th'. Notice that it did not recognize `Thomas' as matching the pattern. The awk language is case sensitive, and matches patterns exactly.
2.4.3 Running Long Programs
Sometimes your awk programs can be very long. In this case it is more convenient to put the program into a separate le. To tell awk to use that le for its program, you type:
awk -f sourcele inputle1 inputle2 : : :
The `-f' instructs the awk utility to get the awk program from the le sourcele. Any le name can be used for sourcele. For example, you could put the program:
/th/
into the le `th-prog'. Then this command:
awk -f th-prog
does the same thing as this one:
awk '/th/'
which was explained earlier (see Section 2.4.2 [Running awk without Input Files], page 16). Note that you don't usually need single quotes around the le name that you specify with `-f', because most le names don't contain any of the shell's special characters. Notice that in `th-prog', the awk program did not have single quotes around it. The quotes are only needed for programs that are provided on the awk command line.
If you want to identify your awk program les clearly as such, you can add the extension `.awk' to the le name. This doesn't a ect the execution of the awk program, but it does make \housekeeping" easier.