sed/awk/grep Cheatsheet - Linux Text Processing Reference
Rather than memorizing three manuals, remember one division of labor: grep narrows the lines, sed rewrites the values, awk sums or groups the fields. This table gives you the daily-reachable subset of each, plus combined pipelines, so log triage and report building stay a few keystrokes instead of a script.
grep Match & Search 10
grep "error" app.logFind lines containing 'error'
grep -i "error" app.logCase-insensitive match
grep -rn "TODO" src/Recursively search a dir, show file and line number
grep -v "debug" app.logInvert match, exclude lines with 'debug'
grep -c "error" app.logCount matching lines
grep -E "error|warning" app.logExtended regex, equivalent to egrep
grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.logPrint only the matched IP address
grep -A 3 "error" app.logShow 3 lines of context after a match
grep -B 2 "error" app.logShow 2 lines of context before a match
grep -f pattern.txt app.logRead multiple patterns from a file
sed Replace & Edit 9
sed 's/old/new/g' file.txtReplace old with new globally
sed -i 's/old/new/g' file.txtEdit the file in place
sed -i.bak 's/old/new/g' file.txtEdit in place and back up the original as .bak
sed '5d' file.txtDelete line 5
sed '/^#/d' file.confDelete all comment lines starting with #
sed '/^$/d' file.txtDelete blank lines
sed -n '10,20p' file.txtPrint only lines 10-20
sed 's/[[:space:]]*$//' file.txtStrip trailing whitespace
sed -E 's/([0-9]+)/\1/' file.txtExtended-regex capture group, -E same as -r
awk Extract & Summarize 9
awk '{print $1}' access.logPrint column 1 (whitespace-delimited by default)
awk -F: '{print $1, $3}' /etc/passwdColon-delimited, print username and UID
awk 'NR==10' file.txtPrint line 10 (NR = line number)
awk 'NR>=10 && NR<=20' file.txtPrint lines 10-20
awk '{sum += $1} END {print sum}' nums.txtSum column 1
awk '{count[$1]++} END {for(k in count) print k, count[k]}' access.logGroup by column 1 and count occurrences
awk '$3 > 100' data.txtPrint rows where column 3 > 100
awk 'length > 80' file.txtPrint rows longer than 80 chars
awk -F: '$3 >= 1000 {print $1}' /etc/passwdList normal users with UID >= 1000
Pipelines 4
grep "error" app.log | awk "{print \$1}" | sort | uniq -c | sort -rn | headTop timestamps by error-log count
cat access.log | awk "{print \$1}" | sort | uniq -c | sort -rn | head -20Top 20 IPs by request volume
grep -oE "[0-9]+ms" app.log | sort -rn | head -10Top 10 response times
sed '/^#/d; /^$/d' nginx.confDelete comment and blank lines in one pass (separate commands with ;)
sort / uniq / comm 10
sort file.txtSort file contents (lexical by default)
sort -n file.txtSort numerically
sort -r file.txtReverse sort
sort -t: -k3 -n /etc/passwdSort by column 3 (colon-delimited) numerically
sort -u file.txtSort and deduplicate
sort file.txt | uniq -c | sort -rn | headClassic combo: top row frequencies
uniq -c file.txtCount consecutive duplicates (sort first)
uniq -d file.txtShow only duplicated lines
comm file1 file2Compare two sorted files: three columns (only file1 / only file2 / both)
diff -u file1 file2Compare files in unified format
cut / tr / paste 10
cut -d: -f1 /etc/passwdExtract column 1 by colon delimiter
cut -c1-10 file.txtExtract characters 1-10 by position
cut -f1,3 file.txtExtract columns 1 and 3 (Tab-delimited by default)
tr 'a-z' 'A-Z' < file.txtLowercase to uppercase
tr -d ' \t' < file.txtDelete all spaces and tabs
tr -s '\n' < file.txtSqueeze consecutive blank lines into one
paste file1.txt file2.txtMerge files side by side (Tab-delimited)
column -t file.txtAlign content into a table
expand file.txtConvert tabs to spaces
command | tee output.txtOutput to both screen and file (common in pipelines)
Tips
- Back up before sed -i edits a file (-i.bak), or preview without -i first.
- For large files, add --line-buffered so grep flushes output live; otherwise the pipe waits for the buffer to fill.
- In awk, $1 inside single quotes gets the column; inside double quotes escape it as \$1 or the shell will expand it.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us