SED & AWK CHEAT SHEET

Text Processing Tools
v1.0
sed Basic Syntax
  • sed 's/a/b/'Replace first a with b
  • sed 's/a/b/g'Replace all a with b
  • sed -i 's/a/b/g'In-place edit
  • sed -n 'p'Silent mode + print
  • sed -e 'cmd1' -e 'cmd2'Multiple commands
sed Commands
  • s/old/new/Substitute
  • dDelete line
  • pPrint line
  • a\textAppend after
  • i\textInsert before
  • c\textReplace line
  • y/abc/xyz/Transliterate
awk Basic Syntax
  • awk '{print}'Print all lines
  • awk '{print $1}'Print first field
  • awk -F: '{...}'Set delimiter
  • awk '/pattern/'Match pattern
  • awk -f script.awkRun script file
awk Variables
  • $0Entire line
  • $1, $2, ...Field 1, 2, etc.
  • NFNumber of fields
  • NRLine number
  • FSField separator
  • OFSOutput field sep
  • RSRecord separator
sed Examples
# Replace first occurrence on each line sed 's/foo/bar/' file.txt # Replace all occurrences globally sed 's/foo/bar/g' file.txt # Delete lines containing pattern sed '/pattern/d' file.txt # Delete lines 5-10 sed '5,10d' file.txt # Print only lines matching pattern sed -n '/pattern/p' file.txt # Replace in-place with backup sed -i.bak 's/foo/bar/g' file.txt
awk Examples
# Print specific columns awk '{print $1, $3}' file.txt # Print with custom separator awk -F':' '{print $1}' /etc/passwd # Sum a column awk '{sum += $2} END {print sum}' file.txt # Filter by condition awk '$3 > 100 {print $1, $3}' data.txt # Count lines matching pattern awk '/error/ {count++} END {print count}' log.txt # Print line numbers awk '{print NR": "$0}' file.txt
sed Address Patterns
  • 5Line 5 only
  • 5,10Lines 5-10
  • /pattern/Matching lines
  • 1,/end/Line 1 to pattern
  • $Last line
  • 1~2Odd lines
awk Operators
  • == != < > <= >=Comparison
  • && || !Logical
  • ~ !~Regex match
  • + - * / %Arithmetic
  • ++ --Increment/Decrement
Advanced Patterns
# sed: Multiple substitutions sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt # sed: Use captured groups sed 's/\(.*\):\(.*\)/\2:\1/' file.txt # Swap around : # awk: BEGIN/END blocks awk 'BEGIN {print "Start"} {print} END {print "End"}' # awk: Conditional printing awk '$3 > 50 && $4 ~ /active/ {print $1}' data.txt # awk: Custom output format awk '{printf "%-10s %5d\n", $1, $2}' data.txt
awk Functions
  • length($0)String length
  • substr(s,i,n)Substring
  • split(s,a,d)Split to array
  • tolower(s)Lowercase
  • toupper(s)Uppercase
  • gsub(r,s)Global replace
sed Flags
  • gGlobal (all matches)
  • iCase insensitive
  • pPrint if changed
  • w fileWrite to file
  • 1, 2, ...Nth occurrence

Scan a script before you share it — free

Finds credentials, personal data and production hostnames. Runs in your tab.

All 51 cheat sheets

Free to read and free to print — no signup. Want them as A4 PDFs you can print? Get all 51 as a PDF bundle for £14.99.
A Martkos IT reference sheet · Blog · Free tools