glunty

sed and awk Cheat Sheet

A searchable reference of common sed and awk one-liners and what each one does.

Common sed and awk one-liners with a plain-English description of what each one does
Command What it does
sed "s/old/new/" Replace the first match of old with new on each line.
sed "s/old/new/g" Replace every match of old on each line (global).
sed "s/old/new/gi" Replace every match, ignoring letter case.
sed "s/old/new/2" Replace only the second match on each line.
sed "s/old/new/2g" Replace from the second match to the end of the line.
sed -i "s/old/new/g" file Edit the file in place instead of printing the result.
sed -i.bak "s/old/new/g" file Edit in place and keep the original as a .bak backup.
sed "s/old/[&]/" Reuse the whole match with the ampersand; here it wraps the match in brackets.
sed "s/\(a\)\(b\)/\2\1/" Capture two groups with escaped parentheses and swap their order.
sed "s|/path|/new|" Use a vertical bar as the delimiter so slashes need no escaping.
sed "s/ *$//" Strip trailing spaces from the end of every line.
sed "s/^/> /" Add a prefix to the start of every line.
sed "2d" file Delete line 2.
sed "2,5d" file Delete lines 2 through 5.
sed -n "5p" file Print only line 5; -n turns off the default output.
sed "/pattern/d" file Delete every line that matches the pattern.
sed -n "/pattern/p" file Print only the lines that match the pattern (like grep).
sed "1,3s/old/new/" file Substitute only on lines 1 through 3.
sed "$d" file Delete the last line of the file.
sed "/start/,/end/d" file Delete the block from a start match through an end match.
sed -n "10,20p" file Print a range of lines, here 10 through 20.
sed -e "s/a/b/" -e "s/c/d/" file Run several scripts in one pass with repeated -e.
sed -E "s/(ab)+/x/" file Use extended regex so groups and plus need no backslashes.
sed "y/abc/xyz/" file Transliterate characters: a to x, b to y, c to z (like tr).
sed "3a hello" file Append a new line reading hello after line 3.
sed "3i hello" file Insert a new line reading hello before line 3.
sed "3c hello" file Change line 3, replacing it with hello.
sed "s/x/y/w out.txt" file Substitute and also write the changed lines to out.txt.
sed -n "$=" file Print the total number of lines (like wc -l).
awk "{print}" file Print every line; this is the default action, like cat.
awk "{print $1}" file Print the first whitespace-separated field.
awk "{print $NF}" file Print the last field on each line using NF.
awk "{print $1, $3}" file Print selected fields separated by a space.
awk -F"," "{print $2}" file Split each line on commas and print the second field.
awk "NR==2" file Print line 2; NR holds the current line number.
awk "END{print NR}" file Print the total line count after the last line.
awk "{print NR, $0}" file Number every line; $0 is the whole line.
awk "/pattern/" file Print the lines that match a regular expression.
awk "!/pattern/" file Print the lines that do not match the pattern.
awk "$3 > 100" file Print rows where the third field is greater than 100.
awk "$1==\"foo\"" file Print rows where the first field equals foo.
awk "NR>1" file Skip the header row and print from line 2 onward.
awk "{sum+=$1} END{print sum}" file Add up the first field and print the total at the end.
awk "{print NF}" file Print how many fields are on each line.
awk "NF==0" file Print the blank lines, which have zero fields.
awk "BEGIN{print \"Header\"}" file Run a BEGIN block once before any input is read.
awk "{printf \"%-10s %s\n\", $1, $2}" file Format output in aligned columns with printf.
awk "BEGIN{OFS=\"-\"} {print $1,$2}" file Set OFS so printed fields are joined with a dash.
awk "{print length($0)}" file Print the character length of each line.
awk "{count[$1]++} END{for(k in count) print k, count[k]}" file Tally values with an associative array and print each key with its count.
awk "{gsub(/old/, \"new\"); print}" file Replace all matches on each line with gsub, then print.
awk "{sub(/old/, \"new\"); print}" file Replace the first match on each line with sub, then print.
awk "length > 80" file Print lines longer than 80 characters using length.

Runs entirely in your browser. Nothing you type is sent anywhere; open DevTools and watch the Network tab to verify zero requests.

What this tool does

This is a searchable quick reference for sed and awk, the two stream-editing workhorses of the Unix command line. sed is built for line-based find and replace and simple line edits, while awk shines at columnar data, where you pull out fields and run small calculations. Each row pairs an exact one-liner with a plain-English note on what it does. Type in the filter box to search across both columns, or tap a category button to focus on one family. The whole list is built into the page, so it works offline and sends nothing anywhere.

How to use it

Start typing in the Filter box. Entering substitute or s/ surfaces the sed replacement recipes; entering field or NF jumps to the awk column tricks; entering delete shows the line-removal commands. The category buttons (sed substitute, sed lines, sed misc, awk basics, awk patterns, awk advanced) narrow the table to one family and combine with the text filter, so you can pick awk advanced and type gsub to zero in on global replacement inside awk. Clear the box to see the full sheet again.

Common use cases

  • Replacing text across a file without opening an editor, using sed -i.
  • Pulling one column out of CSV or log output with awk field variables like $2 and $NF.
  • Deleting or printing a specific range of lines with sed -n.
  • Summing or averaging a numeric column with an awk END block.
  • Cleaning up trailing whitespace, prefixes, or suffixes before piping into another command.

Common pitfalls

  • Shell quoting. In a real shell, wrap sed and awk programs in single quotes so the dollar sign and other characters are not expanded before the tool sees them. This sheet shows double quotes only so the examples stay easy to read.
  • sed -i is not portable. GNU sed accepts sed -i with no argument, but BSD and macOS sed need a suffix, as in sed -i "" .... Use sed -i.bak to keep a backup and stay safe on both.
  • Basic versus extended regex. Plain sed uses basic regular expressions where + and ? must be escaped to act as quantifiers; add -E for the extended regex most other tools use.
  • Field separator surprises. awk splits on runs of whitespace by default, so leading spaces do not create empty fields. Pass -F when your data uses a fixed delimiter such as a comma or a tab.

Frequently asked questions

What is the difference between sed and awk?
Both are line-oriented text processors, but they specialize. sed is a stream editor aimed at find-and-replace and simple line edits: deleting, printing, or substituting text on the lines you select. awk is a small programming language built around fields and records, so it excels at columnar data where you print specific columns, filter rows by a condition, and compute sums or counts. A rough rule: reach for sed to transform text and awk to work with columns and numbers.
How do I replace text with sed?
Use the substitute command: sed "s/old/new/" replaces the first match of old with new on each line, and adding the g flag, sed "s/old/new/g", replaces every match on the line. By default sed prints to standard output and leaves your file untouched, so redirect the result to a new file or add -i to edit the file in place. You can also target specific lines, for example sed "2,5s/old/new/g" to substitute only on lines 2 through 5.
How do I print a column with awk?
awk splits each line into fields, so print the column you want by its number: awk "{print $1}" prints the first field, awk "{print $3}" the third, and awk "{print $NF}" the last field no matter how many there are. Fields are separated by whitespace by default; if your data uses another delimiter, set it with -F, as in awk -F"," "{print $2}" to read the second comma-separated value.
What does the g flag do in sed?
In a sed substitution the trailing g means global: replace every match on a line instead of just the first. Without g, sed "s/a/b/" changes only the first a on each line; with g, sed "s/a/b/g" changes them all. You can combine flags, so gi replaces every match case-insensitively, and a number before g, such as 2g, replaces from the second match to the end of the line. The g flag applies per line, so sed still processes every line in the input.
How do I edit a file in place?
Add the -i flag: sed -i "s/old/new/g" file rewrites the file directly instead of printing to the screen. Because this overwrites the original, it is wise to keep a backup: sed -i.bak "s/old/new/g" file saves the untouched original as file.bak first. Note that -i is a GNU extension; BSD and macOS sed require an explicit suffix argument, so use sed -i "" "s/old/new/g" file there, or install GNU sed.
How do I sum a column of numbers with awk?
Accumulate a running total and print it in an END block: awk "{sum+=$1} END{print sum}" file adds up the first field across every line and prints the total once the input is exhausted. To average instead, divide by the record count, as in awk "{sum+=$1} END{print sum/NR}" file. Change $1 to the field you need, and add -F to set the delimiter if the column is not whitespace-separated.
Does this cheat sheet send anything anywhere?
No. The entire cheat sheet is baked into the page and every filter and category match runs in your browser with JavaScript, so nothing you type is sent to any server. Open your browser DevTools and watch the Network tab while you search to confirm there are zero requests.

Embed this tool

Free for any use; attribution appreciated. Paste this on your site:

The embed runs the same tool that lives at this URL. No tracking; no ads inside the embed. Resize height as needed for your layout.

Cite this tool

For academic, journalistic, or technical references. Pick a format:

Citations use 2026 as the publication year. Access date is left as a fillable placeholder where the citation style expects one.

Embedded tool from glunty.com