awk
Overview
The awk
command is a powerful text processing language for pattern scanning and processing. It treats input as records and fields, making it ideal for structured text manipulation.
Syntax
awk [options] 'program' [input-file]
Common Options
Option | Description |
---|---|
-F fs |
Field separator |
-f file |
Program file |
-v var=val |
Set variable |
-W version |
Show version |
-W help |
Show help |
-W lint |
Check syntax |
-W exec |
Execute only |
-W compat |
Compatibility |
-W copyleft |
Show license |
-W usage |
Show usage |
Built-in Variables
Variable | Description |
---|---|
$0 |
Entire line |
$1-$n |
Field number |
NF |
Number of fields |
NR |
Record number |
FS |
Field separator |
RS |
Record separator |
OFS |
Output field sep |
ORS |
Output record sep |
FILENAME |
Current file |
FNR |
File record number |
Key Use Cases
- Field processing
- Text analysis
- Report generation
- Data extraction
- File transformation
Examples with Explanations
Example 1: Print Fields
awk '{print $1, $3}' file
Print first and third fields
Example 2: Field Separator
awk -F: '{print $1}' /etc/passwd
Print usernames from passwd
Example 3: Pattern Match
awk '/pattern/ {print}' file
Print matching lines
Common Usage Patterns
Sum column:
awk '{sum += $1} END {print sum}'
Count matches:
awk '/pattern/ {count++} END {print count}'
Format output:
awk '{printf "%-10s %s\n", $1, $2}'
Programming Features
- Variables
- Arrays
- Functions
- Control flow
- Regular expressions
Additional Resources
Best Practices
- Use functions
- Comment code
- Test patterns
- Handle errors
- Document usage
Common Functions
- length()
- substr()
- index()
- match()
- split()
Troubleshooting
- Field separation
- Pattern matching
- Variable scope
- Syntax errors
- File handling