Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#sed-awk-cheatsheet

Sed & Awk Cheatsheet

sed (stream editor) and awk (pattern scanning and processing language) are powerful, standard Unix utilities for manipulating text files and streams.

Sed (Stream Editor)

sed is most commonly used for finding and replacing text.

Search and Replace

# Replace first occurrence of 'old' with 'new' on each line
sed 's/old/new/' file.txt
 
# Replace all occurrences (global)
sed 's/old/new/g' file.txt
 
# Ignore case
sed 's/old/new/gi' file.txt
 
# Modify file in-place (save changes)
sed -i 's/old/new/g' file.txt
 
# Modify file in-place and create a backup (.bak)
sed -i.bak 's/old/new/g' file.txt

Deleting Lines

# Delete line 5
sed '5d' file.txt
 
# Delete lines 2 through 4
sed '2,4d' file.txt
 
# Delete lines containing 'pattern'
sed '/pattern/d' file.txt
 
# Delete blank lines
sed '/^$/d' file.txt

Printing Specific Lines

# Print only line 10
sed -n '10p' file.txt
 
# Print lines containing 'pattern'
sed -n '/pattern/p' file.txt

Awk

awk is excellent for processing columnar data and generating reports. By default, it splits lines by whitespace into fields ($1, $2, etc.). $0 represents the whole line.

Printing Fields

# Print the first and third columns
awk '{print $1, $3}' file.txt
 
# Print the last column (NF = Number of Fields)
awk '{print $NF}' file.txt

Filtering Data

# Print lines where the 2nd column equals 'ERROR'
awk '$2 == "ERROR" {print $0}' file.log
 
# Print lines where the 3rd column is greater than 100
awk '$3 > 100 {print $1, $3}' data.txt
 
# Print lines containing 'pattern'
awk '/pattern/ {print $0}' file.txt

Changing the Field Separator

If your data is CSV or colon-separated (like /etc/passwd), use -F.

# Print usernames from /etc/passwd
awk -F: '{print $1}' /etc/passwd
 
# Print first two columns of a CSV
awk -F, '{print $1, $2}' file.csv

Math and Logic

# Sum the values in the first column
awk '{sum += $1} END {print sum}' numbers.txt
 
# Count the number of lines
awk 'END {print NR}' file.txt