sed & awk Command Cheatsheet
sed (stream editor) and awk (text analysis tool) are the two powerhouses of Linux/Unix text processing. sed excels at pattern-based line operations and substitutions; awk shines at field analysis on structured text and report generation. This cheatsheet covers the most commonly used sed and awk commands — from basics to advanced techniques — each with practical examples.
Quick Reference
| Task | Command |
| Find and replace in a file | sed -i 's/old/new/g' file |
| Print lines 5-10 | sed -n '5,10p' file |
| Remove blank and comment lines | sed '/^#/d;/^$/d' file |
| Print 1st and 3rd column of CSV | awk -F',' '{print $1,$3}' file |
| Sum a numeric column | awk '{sum+=$1} END {print sum}' file |
| Filter rows where column > 100 | awk '$NF > 100' file |
| Formatted table output | awk '{printf "%-20s %5d\\n", $1, $NF}' |
sed Basic Operations
``bash
# Text substitution
sed 's/pattern/replacement/' file # Replace first match per line
sed 's/pattern/replacement/g' file # Global replace (all matches)
sed 's/pattern/replacement/2' file # Replace only the 2nd match per line
sed -i 's/pattern/replacement/g' file # In-place edit (GNU sed)
sed -i '' 's/pattern/replacement/g' file # In-place edit (macOS BSD sed)
sed -i.bak 's/pattern/replacement/g' file # Backup then replace (cross-platform)
# Line operations sed -n '5,10p' file # Print lines 5 through 10 sed -n '/pattern/p' file # Print lines matching pattern sed '5d' file # Delete line 5 sed '/pattern/d' file # Delete matching lines sed '$d' file # Delete the last line sed -n '1~2p' file # Print odd lines (every 2 lines starting at 1) sed '2~2d' file # Delete even lines
# Line editing
sed '3a\\new line' file # Append a line after line 3
sed '2i\\new line' file # Insert a line before line 2
sed '5c\\replaced line' file # Replace line 5 with new text
sed '/pattern/r insert.txt' file # Insert file content after matched line
sed '/pattern/w output.txt' file # Write matching lines to a new file
`
sed Advanced Operations
`bash
# Range selection and compound operations
sed -n '/start/,/end/p' file # Print lines between two patterns
sed '/start/,/end/d' file # Delete lines within a range
sed -n '5,/pattern/p' file # From line 5 to first pattern match
sed '0,/pattern/{s/old/new/}' file # Replace only the first match
sed '/pattern/!d' file # Delete non-matching lines (keep matches)
# Multiple commands sed '/pattern/{s/foo/bar/;s/baz/qux/}' file # Execute multiple subs on matched lines sed -e 's/foo/bar/' -e 's/baz/qux/' file # Multiple substitute commands sed 's/foo/bar/g; s/baz/qux/g' file # Semicolon-separated commands (GNU sed)
# Transformation and transliteration sed 's/.*/\\U&/' file # Uppercase entire line sed 's/.*/\\L&/' file # Lowercase entire line sed 's/\\b./\\U&/g' file # Capitalize first letter of each word sed 'y/abc/ABC/' file # Character mapping (a→A, b→B, c→C)
# Using the hold space
sed -n 'h;n;G;p' file # Merge even lines into odd lines
sed '1!G;h;$!d' file # Reverse file order (simulate tac)
`
sed Regex & Patterns
`bash
# Basic regex (BRE)
sed '/^$/d' file # Delete empty lines (^ start, $ end)
sed '/^#/d' file # Delete comment lines starting with #
sed '/^[[:space:]]*$/d' file # Delete whitespace-only lines
sed '/foo.*bar/d' file # Delete lines containing foo followed by bar
sed 's/\\[.*\\]//g' file # Remove brackets and their contents
sed 's/^[[:space:]]*//' file # Strip leading whitespace
sed 's/[[:space:]]*$//' file # Strip trailing whitespace
# Extended regex (ERE) - use -E flag sed -E 's/([0-9]+)/[\\1]/g' file # Wrap numbers in brackets sed -E 's/^([^,]+),([^,]+)/\\2,\\1/' file # Swap first two fields (CSV) sed -E '/^(#|$)/d' file # Delete comments and empty lines sed -E 's/[[:space:]]+/ /g' file # Collapse multiple spaces into one sed -E 's|https?://([^/]+).*|URL: \\1|' file # Extract domain from URL
# Grouping and backreferences
sed -E 's/([a-z]+)@([a-z]+)/\\1 AT \\2/g' file # Obfuscate email format
sed -E 's/(.)(.)/\\2\\1/g' file # Swap adjacent characters
sed -E 's/^(.{50}).*/\\1.../' file # Truncate long lines to 50 chars
`
awk Basic Operations
`bash
# Field printing
awk '{print $1}' file # Print first field (default: space-delimited)
awk '{print $0}' file # Print the entire line
awk '{print $1, $3}' file # Print fields 1 and 3
awk '{print NR, $0}' file # Print with line numbers
awk '{print NF, $0}' file # Show field count per line
# Custom delimiters awk -F',' '{print $1, $3}' file # Comma-separated (CSV processing) awk -F: '{print $1, $6}' /etc/passwd # Colon-separated awk -F'[ ,;]' '{print $1, $2}' file # Multiple delimiters awk 'BEGIN {FS=":"} {print $1}' file # Set delimiter in BEGIN block awk -F'\t' '{print $2}' file # Tab-separated
# Pattern matching awk '/pattern/ {print}' file # Print matching lines awk '/^error/ {print NR, $0}' file # Print lines starting with error + line numbers awk '!/pattern/ {print}' file # Print non-matching lines awk 'NR > 1 {print}' file # Skip header row awk 'NR % 2 == 0 {print}' file # Print even-numbered lines
# Conditional filtering
awk '$3 > 100 {print}' file # Third column > 100
awk '$1 == "OK" {print}' file # First column equals OK
awk 'length($0) > 80 {print}' file # Lines longer than 80 characters
awk '$NF ~ /error/ {print}' file # Last column contains "error"
awk '$1 >= 10 && $1 <= 100' file # Value between 10 and 100
`
awk Advanced Operations
`bash
# Statistics and aggregation
awk '{sum+=$1} END {print "Sum:", sum}' file # Sum a column
awk '{sum+=$1; count++} END {print "Avg:", sum/count}' file # Average
awk '{if ($1 > max) max=$1} END {print "Max:", max}' file # Maximum
awk 'NR==1{min=$1} {if ($1 < min) min=$1} END {print "Min:", min}' file # Minimum
# BEGIN / END blocks awk 'BEGIN {print "Report Start"} {print $0} END {print NR, "lines total"}' file awk 'BEGIN {FS=":"; OFS="--"} {print $1, $3}' /etc/passwd awk 'BEGIN {printf "%-20s %10s\\n", "Name", "Value"; print "------------------------"} {printf "%-20s %10d\\n", $1, $2}' file
# Arrays and counting awk '{a[$1]++} END {for (k in a) print k, a[k]}' file # Count value occurrences awk '{a[$1]+=$2} END {for (k in a) print k, a[k]}' file # Group by first column, sum second awk '!seen[$0]++' file # Remove duplicates (keep first occurrence)
# Multi-file processing awk 'FNR==NR {a[$1]; next} $1 in a' file1 file2 # File intersection (by field) awk 'FNR==NR {a[$1]=$2; next} {print $1, a[$1]}' map.txt data.txt # Key-based replacement awk 'NR==FNR {a[$1]++; next} !a[$1]' all.txt exclude.txt # Exclude items listed in another file
# Shell variables and external commands awk -v threshold=100 '$1 > threshold {print}' file # Pass shell variable awk -v name="$USER" '{print name, $0}' file # Pass environment variable awk '{"date" | getline now; print now, $0}' file # Execute external command awk '{system("ls -l " $1)}' file # Run system command per line
# String functions
awk '{print toupper($0)}' file # Uppercase
awk '{print tolower($0)}' file # Lowercase
awk '{print substr($1, 1, 3)}' file # Substring (first 3 chars)
awk '{print length($0), $0}' file # Show line length
awk '{gsub(/old/, "new"); print}' file # Global in-line substitution
`
awk Field Processing
`bash
# Field selection and restructuring
awk '{print $1, $(NF-1)}' file # Print first and second-to-last columns
awk '{print $NF}' file # Print the last column
awk '{$1=""; print $0}' file # Remove the first column
awk '{$NF=""; $1=""; print $0}' file # Remove first and last columns
awk -v n=3 '{print $n}' file # Dynamic column selection by variable
# Formatted output awk '{printf "%-20s %5d %8.2f\\n", $1, $2, $3}' file # Formatted table output awk 'BEGIN{OFS="|"} {print $1, $2, $3}' file # Set output delimiter to | awk '{print $1 "," $2 "," $3}' file # Manual CSV concatenation awk -v OFS='\t' '{print $1, $2}' file # Tab-separated output
# Field conditions awk '{if ($NF > 100) print}' file # Last column > 100 awk '$1 ~ /^[A-Z]/ {print}' file # First column starts with uppercase awk '{max=($1>$2)?$1:$2; print max}' file # Compare two columns, print larger awk 'NR==1{min=$1} {if ($1 < min) min=$1} END {print "Min:", min}' file # Track minimum value
# Advanced field parsing awk 'BEGIN {FPAT="[^,]*|\\"[^\\"]*\\""} {print $1}' file # Handle quoted CSV fields awk -F'[ ]+' '{print $1, $(NF-1)}' file # Variable-width space delimiter awk '{$1=$1; print}' file # Reformat line (clean up extra whitespace) ``
sed Basics(7)
| Command | Level | ||
|---|---|---|---|
sed 's/pattern/replacement/' fileReplace the first occurrence of a pattern per line | Basic | sed 's/foo/bar/' file.txt | |
sed 's/pattern/replacement/g' fileReplace all occurrences of a pattern globally | Basic | sed 's/foo/bar/g' file.txt | |
sed -i 's/pattern/replacement/g' fileEdit file in-place with substitution | Basic | sed -i.bak 's/oldtext/newtext/g' config.ini | |
sed -n '5,10p' filePrint lines from 5 to 10 | Basic | sed -n '10,20p' /var/log/syslog | |
sed '/pattern/d' fileDelete lines matching a pattern | Basic | sed '/^$/d' file.txt | |
sed '5d' fileDelete a specific line by number | Basic | sed '10d' file.txt | |
sed -n '/pattern/p' filePrint only lines matching a pattern | Basic | sed -n '/ERROR/p' /var/log/app.log |
sed Advanced(6)
| Command | Level | ||
|---|---|---|---|
sed -n '/start/,/end/p' filePrint lines between two patterns (inclusive) | Intermediate | sed -n '/BEGIN/,/END/p' document.txt | |
sed 's/.*/\U&/' fileConvert the entire line to uppercase | Intermediate | sed 's/.*/\U&/' file.txt | |
sed -n '1~2p' filePrint odd-numbered lines (step pattern) | Intermediate | sed -n '0~2p' file.txt | |
sed '/pattern/{s/foo/bar/;s/baz/qux/}' fileExecute multiple substitutions on matched lines | Intermediate | sed '/user/{s/name/id/;s/email/mail/}' config.txt | |
sed '1!G;h;$!d' fileReverse file order using the hold space | Expert | sed '1!G;h;$!d' file.txt | |
sed 'y/abc/ABC/' fileTransliterate characters (character mapping) | Intermediate | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' file.txt |
sed Regex(5)
| Command | Level | ||
|---|---|---|---|
sed -E 's/([0-9]+)/[\1]/g' fileExtended regex with backreferences — wrap all numbers in brackets | Intermediate | sed -E 's/([0-9]+\.[0-9]+)/[\1]/g' data.txt | |
sed -E '/^(#|$)/d' fileDelete comment lines and empty lines | Intermediate | sed -E '/^(#|$)/d' config.conf | |
sed -E 's/[[:space:]]+/ /g' fileCollapse consecutive whitespace into a single space | Intermediate | sed -E 's/[[:space:]]+/ /g' file.txt | |
sed -E 's/^([^,]+),([^,]+)/\2,\1/' fileSwap the first two fields in a CSV | Intermediate | sed -E 's/^([^,]+),([^,]+)/\2,\1/' data.csv | |
sed 's/^[[:space:]]*//; s/[[:space:]]*$//' fileStrip both leading and trailing whitespace | Basic | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' file.txt |
awk Basics(7)
| Command | Level | ||
|---|---|---|---|
awk '{print $1}' filePrint the first field (default space-delimited) | Basic | awk '{print $1}' access.log | |
awk '{print NR, $0}' filePrint each line prefixed with its line number | Basic | awk '{print NR, $0}' file.txt | |
awk -F',' '{print $1, $3}' fileUse comma delimiter, print fields 1 and 3 | Basic | awk -F',' '{print $1, $3}' data.csv | |
awk '/pattern/ {print}' filePrint lines matching a pattern | Basic | awk '/ERROR/ {print}' /var/log/app.log | |
awk 'NR > 1 {print}' fileSkip the first line (header row) | Basic | awk 'NR > 1 {print}' data.csv | |
awk '$3 > 100 {print}' filePrint lines where the third column exceeds 100 | Basic | awk '$3 > 100 {print}' report.txt | |
awk 'length($0) > 80 {print}' filePrint lines longer than 80 characters | Basic | awk 'length($0) > 80 {print}' source.py |
awk Advanced(6)
| Command | Level | ||
|---|---|---|---|
awk '{sum+=$1} END {print sum}' fileSum the values in the first column | Intermediate | awk '{sum+=$1} END {print "Total:", sum}' sales.txt | |
awk '{a[$1]++} END {for (k in a) print k, a[k]}' fileCount occurrences of each unique value in the first column | Intermediate | awk '{a[$1]++} END {for (k in a) print k, a[k]}' ip.log | |
awk '!seen[$0]++' fileRemove duplicate lines (keep first occurrence) | Intermediate | awk '!seen[$0]++' file.txt | |
awk 'FNR==NR {a[$1]; next} $1 in a' file1 file2Find file intersection by key field between two files | Expert | awk 'FNR==NR {a[$1]; next} $1 in a' keys.txt data.txt | |
awk -v threshold=100 '$1 > threshold {print}' filePass a shell variable into an awk script | Intermediate | awk -v limit=500 '$1 > limit {print}' data.txt | |
awk '{gsub(/old/, "new"); print}' filePerform in-line global substitution with gsub() | Intermediate | awk '{gsub(/[0-9]+/, "NUM"); print}' data.txt |
awk Fields(7)
| Command | Level | ||
|---|---|---|---|
awk '{print $NF}' filePrint the last field of each line | Basic | awk '{print $NF}' /etc/passwd | |
awk '{print $1, $(NF-1)}' filePrint the first and second-to-last columns | Intermediate | awk '{print $1, $(NF-1)}' data.txt | |
awk '{$1=""; print $0}' fileRemove the first column (set it to empty string) | Intermediate | awk '{$1=""; print $0}' file.txt | |
awk '{printf "%-20s %5d\\n", $1, $2}' fileFormat output as a right/left-aligned table | Intermediate | awk '{printf "%-20s %8.2f\\n", $1, $2}' report.txt | |
awk 'BEGIN{OFS="|"} {print $1, $2, $3}' fileSet output field separator (OFS), commonly used for formatted table output | Intermediate | awk 'BEGIN{OFS="|"} {print $1, $2, $3}' data.txt | |
awk 'BEGIN {FPAT="[^,]*|\"[^\"]*\""} {print $1}' fileUse FPAT to handle quoted CSV fields with embedded commas | Expert | awk 'BEGIN {FPAT="[^,]*|\"[^\"]*\""} {print $1}' complex.csv | |
awk '{$1=$1; print}' fileReformat a line by cleaning up extra whitespace | Basic | awk '{$1=$1; print}' messy.txt |