The Linux awk Command: Text Processing Power
The Linux awk Command: Text Processing Power
I had a CSV file with 10,000 rows and needed to extract specific columns, sum numbers, and format the output. My colleague said "just use awk." Three hours later, I had a working command—awk is that powerful.
Let me save you that learning curve.
First awk Command
Print all lines
awk '{print}' file.txt
Print specific column
awk '{print $1}' file.txt
$1 = first column, $2 = second, etc.
Print multiple columns
awk '{print $1, $3}' file.txt
Field Separators
Default: whitespace
awk '{print $2}' file.txt
Custom delimiter
awk -F',' '{print $2}' file.csv
Comma-separated.
Multiple delimiters
awk -F'[,|]' '{print $2}' file.txt
Comma or pipe.
Patterns and Conditions
Print matching lines
awk '/error/ {print}' logfile
Print lines containing "error".
Conditional print
awk '$3 > 100 {print}' data.txt
Column 3 > 100.
Beginning and end
awk 'NR==1 {print}' file.txt
awk 'END {print NR}' file.txt
First line or total lines.
Built-in Variables
| Variable | Meaning |
|---|---|
| NF | Number of fields |
| NR | Line number |
| FS | Field separator |
| OFS | Output field separator |
| RS | Record separator |
| ORS | Output record separator |
Using variables
awk '{print NR, $0}' file.txt
Print with line numbers.
awk Commands That Work
Print column 1 and 3
awk '{print $1, $3}' data.txt
Sum column
awk '{sum+=$2} END {print sum}' data.txt
Sum second column.
Format output
awk '{printf "%-10s %5.2f\n", $1, $2}' data.txt
Formatted output.
Conditional formatting
awk '$2 > 100 {print $1, "HIGH"}' data.txt
Filter by pattern
awk -F',' '/ERROR/ {print $1, $5}' logfile
The awk Command Builder
Building awk commands—the awk Command Builder:
- Field selector
- Pattern builder
- Output formatter
Common Patterns
CSV processing
awk -F',' '{print $1}' data.csv
Log parsing
awk '/ERROR/ {print $1, $4}' logfile
Column totals
awk '{sum+=$2} END {print sum}' data.txt
Average
awk '{sum+=$2; count++} END {print sum/count}' data.txt
Lessons Learned
-
$0 is entire line — $1, $2 are columns.
-
Use -F for delimiters — default is whitespace.
-
** BEGIN/END special** — before/after processing.
-
printf for formatting — controlled output.
-
awk is fast — great for large files.
Conclusion: awk Is Your Text Tool
awk is the Swiss Army knife of text processing—master it.
The awk Command Builder makes building awk easy.
