grep, sed and awk for Beginners, with Examples
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.
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:
-iignores case:grep -i apple fruit.txtprintsapple 3andApple 2.-nadds line numbers:grep -n cherry fruit.txtprints4:cherry 4.-vinverts the search, printing the lines that do not match.-ccounts matching lines:grep -ci apple fruit.txt, which is-cplus-i, prints 2.-rsearches a folder and its subfolders, with the file name before each match, as innotes/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.txtfindsbanana 5.$means the end of a line:grep '4$' fruit.txtfindscherry 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.txtprints the first column: apple, banana, Apple, cherry.awk '{sum += $2} END {print sum}' fruit.txtprints 14. Each line adds field 2 to sum, which starts empty and counts as zero, andENDprints 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.
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/^ *//'.
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.