Course contents · Lesson 9 of 19

grep, sed and awk for Beginners, with Examples

The Command Line · Lesson 9 of 19 · 9 min Foundations

Your web server's log has fifty thousand lines, and you need to know how many visitors hit an error today. This lesson shows grep, sed and awk, three classic commands that find lines, replace text and pick out columns, and chains them into one answer.

Three tools, three jobs

grep, sed and awk come with macOS and almost every Linux system. Each reads text line by line, from the files you name or, with none named, from stdin, standard input (lesson 8). Each writes its result to stdout, standard output: the screen, unless you redirect or pipe it.

So your file stays as it was; to keep a result, send it to a new file with >. The flowchart matches each job to a tool. Notice the last row: a job of several steps chains the tools with the vertical bar.

Flowchart. Your text, a file or another command's output, leads to three jobs. Find matching lines, like Ctrl+F for every line, leads to grep, as in grep -i error app.log. Replace some text, like find-and-replace, leads to sed, as in sed 's/old/new/g' file. Pick or add up a column, like spreadsheet columns, leads to awk, as in awk '{print $1}' file. A footer says that for several jobs you chain the tools with the vertical bar, as in grep, then awk, then sed
Pick the tool by the job: grep finds lines, sed replaces text and awk takes columns.

grep: find the lines that match

The examples use fruit.txt, an illustrative file of four lines: apple 3, banana 5, Apple 2 and cherry 4.

grep apple fruit.txt prints every line that contains the pattern, the text you search for. Here that is only apple 3, because grep treats capitals as different letters.

Options change what grep shows:

  • -i ignores case: grep -i apple fruit.txt prints apple 3 and Apple 2.
  • -n adds line numbers: grep -n cherry fruit.txt prints 4:cherry 4.
  • -v inverts the search, printing the lines that do not match.
  • -c counts matching lines: grep -ci apple fruit.txt, which is -c plus -i, prints 2.
  • -r searches a folder and its subfolders, with the file name before each match, as in notes/shop.txt:apple 3 (illustrative).

A gentle word on patterns

A grep pattern is a regular expression, or regex: text in which a few characters have special meanings. Four cover most everyday searches:

  • ^ means the start of a line: grep '^b' fruit.txt finds banana 5.
  • $ means the end of a line: grep '4$' fruit.txt finds cherry 4.
  • . means any single character: 'b.n' matches the ban in banana.
  • * repeats the character before it any number of times, even zero: 'ap*le' matches ale, aple and apple, and .* matches anything.

Put patterns in single quotes, so the shell, the program that reads your commands, passes them to grep untouched instead of treating * as a file-name wildcard. For a real dot, as in an IP address (a computer's network address), write \. instead.

sed: find and replace, line by line

sed is the stream editor: it edits lines as they stream past from a file or a pipe. Its everyday use is sed 's/old/new/g' file. Read it as s for substitute, then the pattern to find, the replacement and the flags, separated by slashes.

Without the g (global) flag, sed replaces only the first match on each line. echo banana | sed 's/a/o/' prints bonana; with g, it prints bonono. Lines with no match are printed unchanged.

Because the old part is a pattern, sed 's/^ *//' deletes the spaces at the start of each line, which the log example below needs. sed only prints. Its -i option edits a file in place, but it works differently on macOS and Linux, so read man sed first.

awk: split lines into columns

awk splits each line into fields, its word for columns, at each run of spaces or tabs, and names them $1, $2 and so on; $0 is the whole line. Write the instructions in braces inside single quotes, so the shell leaves the $ signs alone.

  • awk '{print $1}' fruit.txt prints the first column: apple, banana, Apple, cherry.
  • awk '{sum += $2} END {print sum}' fruit.txt prints 14. Each line adds field 2 to sum, which starts empty and counts as zero, and END prints it after the last line.

The table runs one file through each tool. Notice the shapes: grep keeps fewer lines, sed changes text inside lines, and awk keeps part of every line.

A table built on the illustrative file fruit.txt, with four lines: apple 3, banana 5, Apple 2, cherry 4. grep -i apple fruit.txt prints apple 3 and Apple 2 and drops banana 5 and cherry 4. sed 's/apple/pear/' fruit.txt prints all four lines with apple 3 changed to pear 3, while Apple 2 stays because of its capital A. awk '{print $2}' fruit.txt prints 3, 5, 2 and 4. A note says fruit.txt never changes, and a footer sums up: grep gives fewer lines, sed changed text, awk fewer columns
grep removes rows, sed edits text inside rows, and awk keeps only the columns you ask for.

All three together on an access log

A web server's access log gets one line per request, that is, per page or file a browser asked for. Many servers use a layout like this illustrative line: 203.0.113.7 - - [02/Dec/2026:10:15:32 +0000] "GET /checkout HTTP/1.1" 500 312. It holds the visitor's address, the time, the request, the status code and the reply's size.

The status code is the server's three-digit answer (What Is HTTP?): codes starting with 2 mean success, 4 a problem with the request, such as 404 Not Found, and 5 an error on the server. The time holds a space, so awk sees the status code as field 9.

Shoppers say checkout fails. In an illustrative log of eight lines, five are for /checkout, and this chain counts their status codes: grep '/checkout' access.log | awk '{print $9}' | sort | uniq -c | sed 's/^ *//'.

Top: an illustrative access-log line, 203.0.113.7 - - [02/Dec/2026:10:15:32 +0000] GET /checkout HTTP/1.1 in quotes, then 500 312, with its space-separated fields numbered $1 to $10. The time takes two fields, the page /checkout is $7 and the status code 500 is $9. Below, five stages. 1, grep '/checkout' access.log keeps 5 of the 8 lines. 2, awk '{print $9}' prints 200, 500, 200, 200, 500. 3, sort gives 200, 200, 200, 500, 500. 4, uniq -c gives 3 200 and 2 500, with spaces in front. 5, sed 's/^ *//' deletes those spaces, leaving 3 200 and 2 500: 3 OK and 2 server errors
Each tool does one step: grep picks rows, awk picks a column, sort and uniq count, and sed tidies.

sort and uniq -c do the counting, as in lesson 8, and sed deletes the spaces uniq puts before each count. Three 200s and two 500s mean two in five checkouts failed on the server.

Check yourself

You can see Error: disk full in app.log, yet grep error app.log prints nothing. Why, and what would find it?

grep treats capitals as different letters, so error does not match Error. grep -i error app.log ignores case and finds the line.

A line reads cat sat by the cat. Why does sed 's/cat/dog/' turn it into dog sat by the cat?

Without the g flag, sed replaces only the first match on each line. sed 's/cat/dog/g' replaces both.

In the access-log line, why does awk '{print $7}' print /checkout and not the status code?

awk splits at spaces, and the time contains one, so it fills fields 4 and 5. That pushes the page to field 7 and the status code to field 9.