Skip to main content

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.

Updated: 2026-07-16·38 commands

Quick Reference

TaskCommand
Find and replace in a filesed -i 's/old/new/g' file
Print lines 5-10sed -n '5,10p' file
Remove blank and comment linessed '/^#/d;/^$/d' file
Print 1st and 3rd column of CSVawk -F',' '{print $1,$3}' file
Sum a numeric columnawk '{sum+=$1} END {print sum}' file
Filter rows where column > 100awk '$NF > 100' file
Formatted table outputawk '{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)

CommandLevel
sed 's/pattern/replacement/' file
Replace the first occurrence of a pattern per line
Basic
sed 's/pattern/replacement/g' file
Replace all occurrences of a pattern globally
Basic
sed -i 's/pattern/replacement/g' file
Edit file in-place with substitution
Basic
sed -n '5,10p' file
Print lines from 5 to 10
Basic
sed '/pattern/d' file
Delete lines matching a pattern
Basic
sed '5d' file
Delete a specific line by number
Basic
sed -n '/pattern/p' file
Print only lines matching a pattern
Basic

sed Advanced(6)

CommandLevel
sed -n '/start/,/end/p' file
Print lines between two patterns (inclusive)
Intermediate
sed 's/.*/\U&/' file
Convert the entire line to uppercase
Intermediate
sed -n '1~2p' file
Print odd-numbered lines (step pattern)
Intermediate
sed '/pattern/{s/foo/bar/;s/baz/qux/}' file
Execute multiple substitutions on matched lines
Intermediate
sed '1!G;h;$!d' file
Reverse file order using the hold space
Expert
sed 'y/abc/ABC/' file
Transliterate characters (character mapping)
Intermediate

sed Regex(5)

CommandLevel
sed -E 's/([0-9]+)/[\1]/g' file
Extended regex with backreferences — wrap all numbers in brackets
Intermediate
sed -E '/^(#|$)/d' file
Delete comment lines and empty lines
Intermediate
sed -E 's/[[:space:]]+/ /g' file
Collapse consecutive whitespace into a single space
Intermediate
sed -E 's/^([^,]+),([^,]+)/\2,\1/' file
Swap the first two fields in a CSV
Intermediate
sed 's/^[[:space:]]*//; s/[[:space:]]*$//' file
Strip both leading and trailing whitespace
Basic

awk Basics(7)

CommandLevel
awk '{print $1}' file
Print the first field (default space-delimited)
Basic
awk '{print NR, $0}' file
Print each line prefixed with its line number
Basic
awk -F',' '{print $1, $3}' file
Use comma delimiter, print fields 1 and 3
Basic
awk '/pattern/ {print}' file
Print lines matching a pattern
Basic
awk 'NR > 1 {print}' file
Skip the first line (header row)
Basic
awk '$3 > 100 {print}' file
Print lines where the third column exceeds 100
Basic
awk 'length($0) > 80 {print}' file
Print lines longer than 80 characters
Basic

awk Advanced(6)

CommandLevel
awk '{sum+=$1} END {print sum}' file
Sum the values in the first column
Intermediate
awk '{a[$1]++} END {for (k in a) print k, a[k]}' file
Count occurrences of each unique value in the first column
Intermediate
awk '!seen[$0]++' file
Remove duplicate lines (keep first occurrence)
Intermediate
awk 'FNR==NR {a[$1]; next} $1 in a' file1 file2
Find file intersection by key field between two files
Expert
awk -v threshold=100 '$1 > threshold {print}' file
Pass a shell variable into an awk script
Intermediate
awk '{gsub(/old/, "new"); print}' file
Perform in-line global substitution with gsub()
Intermediate

awk Fields(7)

CommandLevel
awk '{print $NF}' file
Print the last field of each line
Basic
awk '{print $1, $(NF-1)}' file
Print the first and second-to-last columns
Intermediate
awk '{$1=""; print $0}' file
Remove the first column (set it to empty string)
Intermediate
awk '{printf "%-20s %5d\\n", $1, $2}' file
Format output as a right/left-aligned table
Intermediate
awk 'BEGIN{OFS="|"} {print $1, $2, $3}' file
Set output field separator (OFS), commonly used for formatted table output
Intermediate
awk 'BEGIN {FPAT="[^,]*|\"[^\"]*\""} {print $1}' file
Use FPAT to handle quoted CSV fields with embedded commas
Expert
awk '{$1=$1; print}' file
Reformat a line by cleaning up extra whitespace
Basic

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-16.