Bash
awk
awk commands for text processing and data extraction.
28 commands
Windows
MacOS
Linux
#text-processing
#data-processing
Basic Patterns
Print every line in the file
awk '{print}' file.txt
Print lines matching a pattern
awk '/pattern/' file.txt
Print lines not matching a pattern
awk '!/pattern/' file.txt
Print only line 5
awk 'NR==5' file.txt
Print lines 3 through 7
awk 'NR>=3 && NR<=7' file.txt
Field Processing
Print the first field of each line
awk '{print $1}' file.txt
Print the first and third fields
awk '{print $1, $3}' file.txt
Use colon as field separator
awk -F: '{print $1}' /etc/passwd
Parse CSV, print second column
awk -F, '{print $2}' data.csv
Print the last field of each line
awk '{print $NF}' file.txt
Print the second-to-last field
awk '{print $(NF-1)}' file.txt
Built-in Variables
Print line number and full line
awk '{print NR, $0}' file.txt
Print number of fields per line
awk '{print NF}' file.txt
Print total number of lines
awk 'END{print NR}' file.txt
Print filename with each line
awk '{print FILENAME, $0}' *.txt
Set field separator in BEGIN
awk 'BEGIN{FS=":"} {print $1}' /etc/passwd
Calculations
Sum values in column 1
awk '{sum += $1} END{print sum}' nums.txt
Calculate average of column 1
awk '{sum += $1} END{print sum/NR}' nums.txt
Find maximum value
awk 'BEGIN{max=0} $1>max{max=$1} END{print max}' nums.txt
Formatted total
awk '{total += $3} END{printf "Total: %.2f\n", total}' data.txt
Output Formatting
Left-aligned formatted output
awk '{printf "%-20s %s\n", $1, $2}' file.txt
Zero-padded line numbers
awk '{printf "%05d %s\n", NR, $0}' file.txt
Set output field separator
awk 'BEGIN{OFS=","} {print $1, $2, $3}' file.txt
Replace text in all fields
awk '{gsub(/old/, "new"); print}' file.txt
Convert whitespace-delimited to CSV
awk '{$1=$1}1' OFS="," file.txt
Quick Commands
Print the first field of each line
awk '{print $1}' file.txt
Parse colon-delimited file and print first field
awk -F: '{print $1}' /etc/passwd
Sum all values in the first column
awk '{sum += $1} END{print sum}' file.txt