Linux Command Reference ยท Concepts
grep vs sed vs awk
Learn when to reach for grep, sed or awk: filtering lines, rewriting text, and working with columns, with side-by-side examples of the same job in each tool.
grep, sed and awk are the three classic text tools of the shell. They all read lines, they all understand patterns, and they overlap a lot, which makes it hard to know which one to use. A simple rule of thumb settles most cases: grep selects lines, sed rewrites text, awk computes with fields.
None of them is "better". They sit on a ladder of power. grep does one thing and does it fast. sed adds editing. awk is a small programming language with variables, arithmetic and arrays. Pick the lowest rung that solves your problem, because simpler commands are easier to read and often faster.
grep 'error' app.log # keep matching lines sed 's/error/ERROR/' app.log # change text in each line awk '$3 > 100 { print $1 }' app.log # test a column, print another
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
What each tool is good at
grep: find lines
grep answers the question "which lines match?". It prints them unchanged, or counts them (-c), lists file names (-l), shows context (-A, -B, -C) or inverts the test (-v). It also returns an exit status (0 for a match, 1 for none), so it doubles as a condition in if statements. When you only need to filter, nothing is shorter or faster. See grep.
sed: edit a stream
sed applies commands to each line as it flows by. Its signature move is substitution (s/old/new/), but it can also delete, insert, print line ranges and edit files in place with -i. Use sed when the output is "the input, with changes". See sed.
awk: split into fields and calculate
awk splits every line into fields ($1, $2, ... and NF) and lets you test them, do arithmetic, keep running totals and report in an END block. Use it when the data is columnar or when you need to remember something between lines, such as sums, counts or "seen before" tracking. See awk.
Decision guide
| You want to... | Best tool |
|---|---|
| Keep or drop lines that match a pattern | grep |
| Count matching lines | grep -c |
| Search many files recursively | grep -r |
| Replace text inside lines | sed |
| Delete or print a line range (lines 10 to 20) | sed |
| Edit a file in place | sed -i |
| Print selected columns | awk (or cut for fixed delimiters) |
| Sum, average or count by key | awk |
| Filter on a number in a column | awk |
| Reformat records (reorder columns, add text) | awk |
Usage notes
- Start with the simplest tool. If
grepcan do it, usegrep. Reach forsedwhen you need to change text, and forawkwhen columns or arithmetic are involved. - They combine well.
grepfirst to narrow the input, thenawkto compute is a very common pair. A pipeline of small tools is usually clearer than one clever script. See The Unix philosophy. - Regular expression flavors differ.
grepandseddefault to basic regular expressions (BRE);-Eswitches to extended (ERE).awkuses ERE. See BRE, ERE and PCRE. - awk is not just for columns.
awk 'length > 80'andawk 'NR % 2'work with no fields at all. Likewisesed -n '5p'prints one line without any pattern. - Whitespace splitting is the default in awk. Runs of spaces and tabs count as one separator. Use
-F,for commas, but note that awk does not understand quoted CSV fields. - Do not use
cat file | grep. Pass the file name as an argument; all three tools accept files directly. - In-place editing differs by system. GNU
sed -itakes no argument; BSD and macOSsed -irequires a suffix argument. Scripts that must run on both should write to a temporary file instead. - Speed. For plain filtering
grepis usually the fastest,sedis close, andawkis slower per line but is rarely the bottleneck. Choose for clarity first. - When none of them fits, structured formats such as JSON or CSV with quoted fields are better handled by dedicated tools (
jq,csvkit) or a scripting language.
Related
grep,sed,awk,cut,sort- BRE, ERE and PCRE, Useful one-liners
- Lessons: grep, sed, awk, Text pipelines