Overview
awk reads input as records (default: lines), divides them by a field separator (FS), then applies a pattern/action { ... } to output the desired result (OFS). Common uses include column extraction, conditional filtering, sum/average calculation, and format conversion.
Key Built-in Variables
These are key built-in variables that control I/O operations and formatting.
- NR: Record (line) number read so far
- FNR: Record number in the current file
- NF: Number of fields in the current record
- FS: Input field separator (default space)
- OFS: Output field separator (default space)
- RS: Record separator (default newline)
- ORS: Output record separator (default newline)
Basic Syntax
awk '패턴 { 액션 }' input.txt
awk -f program.awk input.txt
Pass the pattern/action block as a single line, or provide a script file with -f.
Key Options
Commonly used options in practice, grouped by scenario. Includes gawk-compatible options.
1) Basic Execution/Input
2) Field/Variable Passing
3) Debugging/Compatibility (gawk)
4) Help/Version
Generated command:
Try combining the commands.
Description:
`awk` Executes the command.
Combine the above options to virtually execute commands with AI.
Common Patterns
Column Extraction
awk -F ':' '{ print $1, $3 }' /etc/passwd
Outputs only the 1st and 3rd fields, separated by colons.
Conditional Filter
awk '$5 > 100 { print }
Outputs only records where the 5th field value is greater than 100.
Aggregation Example
awk '{ sum += $2 } END { print sum }' data.txt
Sum of the second column
Variable Passing and Formatting
awk -v OFS=',' '$1=="KEY" { print $1, $2, $3 }' input.txt
Specifies OFS as a comma and outputs only specific keys.
Practical Tips
As it processes streams line by line, it's powerful when combined with pipes.
- Regular expressions can be used for field separators: -F '[,:\t]+'
- Programs can be written in parts using multiple -e options
- Separate long scripts with -f and manage versions with git
- Adjust OFS/ORS appropriately for output formatting
- For large inputs, simplify the pipeline by leaving only the essential operations