print statement to extract and format data.
Awk Usage Overview
| Option | Description |
|---|---|
-F fs | Set the input field separator to fs |
-v var=value | Assign a value to an awk variable before program execution |
-f file | Read the awk program from the specified file |
program | Provide the awk program directly as a quoted string |
file... | One or more input files; if omitted, reads from standard input |
Always quote your
program (single or double quotes) so the shell passes it verbatim to awk.Pattern-Action Structure
An awk program is a sequence of pattern-action pairs:- If pattern is omitted, action runs on every input line.
- The
{ … }block is the action block, containing commands likeprint, loops, and conditionals.
Without quotes around
{}, many shells will interpret braces or special characters—always quote your action blocks!The print Statement
Inside the action block, print sends its arguments (fields, string literals, variables) to standard output.
Accessing Fields
By default, awk splits each line on whitespace into fields named$1, $2, …, $NF.
Processing Files
Place the filename after the program to read from a file instead of interactively: Givenabc.txt:
Printing String Literals
You can mix fields and literal strings in a singleprint:
Multiple Expressions
Separate expressions by commas; awk joins them with the output field separator (OFS, default is a space):
Redirecting and Piping Input
Awk accepts input from:- Files:
- Standard input via redirection:
- Piping from other commands:
Summary
- Command structure:
awk [options] [pattern-action] [file...] - Pattern-action:
pattern { action } - Fields:
$1,$2, …$NF - String literals: printed as-is within quotes
- Separators: input (
FS) and output (OFS) - Interactive mode: omit files; end with Ctrl-D, cancel with Ctrl-C