GNU Awk Cheatsheet
#
GNU Awk (gawk
) is the GNU implementation of the awk
programming language, providing additional features beyond the POSIX standard.
Basic Syntax
#
Command/Option | Example | Description |
---|
gawk '{print $1}' file.txt | gawk '{print $1}' data.txt | Print the first field of each line from the file |
gawk '/pattern/' file.txt | gawk '/error/' log.txt | Print lines that match the pattern |
gawk '{print $1, $3}' file.txt | gawk '{print $1, $3}' data.txt | Print the first and third fields from each line |
gawk -F, '{print $1}' file.csv | gawk -F, '{print $1}' data.csv | Print the first field from a comma-separated file |
Field Separators
#
Command/Option | Example | Description |
---|
gawk -F ' ' '{print $1}' file.txt | gawk -F ' ' '{print $1}' data.txt | Set space as the field separator |
gawk -F ':' '{print $1}' file.txt | gawk -F ':' '{print $1}' passwd.txt | Set colon as the field separator |
Patterns and Actions
#
Command/Option | Example | Description |
---|
gawk '$3 > 50' file.txt | gawk '$3 > 50' data.txt | Print lines where the third field is greater than 50 |
gawk '{sum += $2} END {print sum}' file.txt | gawk '{sum += $2} END {print sum}' sales.txt | Sum the values in the second field and print the total |
gawk 'BEGIN {FS=","; OFS="\t"} {print $1, $2}' file.csv | gawk 'BEGIN {FS=","; OFS="\t"} {print $1, $2}' data.csv | Set input field separator to comma and output field separator to tab |
Built-in Variables
#
Command/Option | Example | Description |
---|
gawk 'NR==1 {print $0}' file.txt | gawk 'NR==1 {print $0}' data.txt | Print the first line of the file (NR is the record number) |
gawk 'END {print NR}' file.txt | gawk 'END {print NR}' data.txt | Print the number of lines in the file |
Extensions and Features
#
Command/Option | Example | Description |
---|
gawk --lint script.awk | gawk --lint process.awk | Enable warnings about deprecated features |
gawk --version | gawk --version | Display the version of GNU Awk |
This cheatsheet provides essential GNU Awk (gawk
) commands for text processing and data extraction.