Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

sed, awk, and grep

Three classic text-processing tools that work beautifully together, especially in pipelines. Each does one job well, and most real-world shell one-liners are just two or three of them chained with pipes. A quick way to remember the division of labor:

  • grep - find the lines that match a pattern.

  • sed - edit a stream of text (substitute, delete, insert).

  • awk - process structured/columnar data (fields, calculations, reports).

All three read their input line by line and are commonly chained with pipes (|). See Common Commands > Redirection, pipes, and wildcards for the pipe and redirection basics these examples rely on.

A note on regular expressions

grep and sed both match text with regular expressions (regex) - a mini language for describing patterns. The essentials:

Pattern
Matches

.

any single character

*

zero or more of the preceding item

^ / $

start / end of the line

[abc]

any one of a, b, or c; [^abc] = none of them

[0-9]

any digit (a range)

\

escapes a special character so it's literal (e.g. \.)

There are two dialects. Basic regex (BRE) is the default in grep and sed and requires backslashes before +, ?, |, (, {. Extended regex (ERE), enabled with grep -E or sed -E, treats those as special without the backslash - usually what you want when a pattern gets complicated.

grep (Global Regular Expression Print)

Searches text for patterns (literal strings or regex) and prints the lines that match - or, with options, the non-matching lines, a count, filenames, and more.

Examples

Common flags: -i ignore case, -n line numbers, -v invert match, -r recursive, -c count, -w whole word, -o only the match, -E extended regex, -A/-B/-C lines of context after/before/around.

sed (Stream Editor)

Edits a text stream line by line - most often used for find-and-replace, but also for deleting, inserting, and printing specific lines. By default it prints the (possibly modified) stream to standard output and leaves your file untouched.

Examples

-i rewrites the file in place - there is no undo, so test without -i first (or use -i.bak). The substitution syntax is s/pattern/replacement/flags; you can swap the delimiter when your text contains slashes, e.g. s|/usr/local|/opt|g - much more readable than escaping every /.

awk

A pattern-scanning and processing language. It's ideal for working with columns, doing calculations, and generating reports. awk reads input line by line, splits each line into fields (by whitespace by default), and runs your actions when a pattern matches. Fields are referenced as $1, $2, …, and $0 is the whole line.

The general shape is awk 'pattern { action }' - either part is optional. A pattern with no action prints matching lines; an action with no pattern runs on every line.

Examples

Useful built-in variables: NR (current record/line number), NF (number of fields on the line), FS (input field separator, set with -F), OFS (output field separator). A BEGIN { } block runs before any input, and END { } runs after all input - handy for printing headers and totals.

Putting them together

The real payoff is chaining them. A few representative pipelines:

sort and uniq -c aren't part of this trio, but they show up constantly alongside it - sort | uniq -c | sort -rn is the canonical "count and rank" idiom.

Last updated