grep and awk — Finding Lines and Working with Fields
Everything in this section builds up to two tools you'll reach for daily. grep answers "which lines mention this?", across one file or a thousand. awk answers "from those lines, take these fields and do some arithmetic". Together they cover most quick questions you'll ever ask about text.
The rule
grep finds the lines; awk picks the fields and does the maths.
grep PATTERN FILE prints every line that contains the pattern:
$ grep chilli harvest.csv
2026-06-03,chilli,0.8,ashaThe options you'll use most:
| Option | Does |
|---|---|
-i |
ignore case, so TOMATO matches tomato |
-v |
invert: print lines that do not match |
-c |
print only the count of matching lines |
-n |
show line numbers |
-r |
search every file in a folder, recursively |
-E |
extended patterns, such as basil or chilli written with a bar between them |
-w |
match whole words only |
$ grep -c tomato harvest.csv # 3
$ grep -n chilli harvest.csv # 5:2026-06-03,chilli,0.8,asha
$ grep -r "TODO" ~/garden # every TODO in every fileawk reads a line at a time, splits it into fields $1, $2 and so on ($0 is the whole line), and runs a small program on it. -F, sets the separator. The program is written as a pattern and an action: condition { what to do }.
$ awk -F, '{ print $2 }' harvest.csv # like cut -d, -f2
$ awk -F, 'NR > 1 && $3 > 1 { print $4 }' harvest.csv # who picked more than 1 kg
asha
ravi
meera
$ awk -F, '$2 == "tomato" { s += $3 } END { print s }' harvest.csv
9.6END { ... } runs once, after the last line. That's where totals get printed. NR is the current line number, so NR > 1 skips the header. Without it, awk would compare the word kg with 1 as text, and the header's picker would sneak into the output. && means "and".
The shortcut
Split any question into which lines and what to do with them. If you only need "which lines", grep is enough. As soon as you need a particular column, a numeric comparison or a total, write it in awk as pattern { action }.
Which lines?
Rows where the picker (field 4) is
asha:$4 == "asha".
Why not use grep asha for this? A grep for "asha" matches the text anywhere on the line. It would also catch a row picked by someone called "natasha". awk's $4 == "asha" tests exactly the right column.
Drills
Answer fast — trust the rule.
Which command prints every line of harvest.csv that does NOT contain tomato?
What does grep -c basil harvest.csv print?
In awk -F, '{ print $3 }' harvest.csv, what is $3 on the second line?
What does awk -F, 'NR > 1 { s += $3 } END { print s }' harvest.csv print?
Which tool fits "list the pickers who picked more than 3 kg in a single pick"?
Remember
- grep finds the lines; awk picks the fields and does the maths.
- Split the question into which lines and what to do with them. awk writes that as
pattern { action }, withENDfor totals. - grep matches text anywhere on the line, so use awk's
$N == "value"when the column matters.
