Topic 5 of 5 · 4 min read
Text pipelines to verify agent claims
Goal: Write a short pipeline that counts error types in a log to check an agent's summary of a failure.
When an agent says "the failures are all timeouts", count them yourself with a short pipeline. A pipeline takes the text a command prints and passes it through small tools, each doing one job. It takes a minute and turns a claim into evidence.
Check yourself · 1 of 3
Which pipeline counts how many times each distinct line appears in words.txt?
Why it matters
Agents summarise logs by reading a slice of them. They may look at the first 200 lines, round a number or describe the most recent error as the main one. A count from the full file does not depend on the summary being right.
The building blocks
- grep keeps matching lines.
grep ERROR app.log. Use-cto count lines,-vto invert,-Efor extended patterns. - cut and awk pick fields.
awk '{print $5}'prints the fifth space-separated column.awk -F, '{print $2}'splits on commas. - sort orders lines. uniq -c counts neighbouring duplicates, so always put
sortbefore it. - head / tail show the start or the end.
sort -rn | headgives the top few. - wc -l counts lines.
- jq does the same for JSON:
jq '.items[] | .status'pulls a field from every item, andjq -rprints raw text without quotes.
Before and after
The agent reports: "The job failed 3 times with a database timeout." The log has lines like 2026-12-01T09:14:02Z ERROR payments timeout connecting to db.
Weak check: grep timeout job.log | head, which shows a few matching lines and no total.
Fixed:
grep ' ERROR ' job.log | awk '{ $1=""; print }' | sed 's/[0-9]\+/N/g' | sort | uniq -c | sort -rn | head
This keeps error lines, drops the timestamp, replaces numbers so similar messages group, then counts each message and shows the most common first. You may find 212 timeout lines and 40 auth failed lines, which is not what the agent said.
For JSON output from an API:
jq -r '.results[] | select(.ok == false) | .code' response.json | sort | uniq -c
What goes wrong
- uniq without sort. Duplicates not next to each other are counted as separate groups.
- Matching too loosely.
grep erroralso hits "no error" and file names. Match the level field. - Checking a sample.
headortailshows a window of the file, not the whole story. Count first, then read examples.
Save the pipeline with the agent's task so a reviewer can run it again.
Examples
Checking a 'mostly one error' claim
grep -c ' ERROR ' job.log
grep ' ERROR ' job.log | awk '{print $4}' | sort | uniq -c | sort -rn
The first line gives the total. The second counts errors by the fourth field, the component name, so you can see whether payments really dominates or the agent only looked at the last lines.
Counting failed items in JSON
jq '[.results[] | select(.ok == false)] | length' response.json
jq -r '.results[].code' response.json | sort | uniq -c
The first command prints how many results failed. The second shows how often each code appears.
Key points
- Count from the full log yourself instead of trusting an agent's summary of a slice.
- Put sort before uniq -c, because uniq only merges lines that are next to each other.
- Use grep -c for a quick count and sort -rn | head for the most common items.
- Use jq to select and count fields in JSON responses, with -r for raw text.
- Match the log level field, not just the word error, so you do not count unrelated lines.
Common mistakes
- Running uniq -c without sort first and reading the split counts as the real totals.
- Using grep error and counting lines that say no error or contain the word in a file name.
- Checking only head or tail output and calling it the whole picture.
- Trusting a pipeline you did not read; each stage should do one clear thing.
Check yourself · 2 of 3
An agent says most errors in app.log are timeouts. Lines look like 2026-12-01T09:14:02Z ERROR db timeout. Which command best checks the claim?
Check yourself · 3 of 3
Which tool is the best fit for printing the status field of every item in a JSON API response?
Practice items, not counted towards your mastery.
Start here
- The Missing Semester of Your CS Education (MIT) (opens in a new tab)CourseFree
Free lectures on the shell, shell scripting, data wrangling and command-line tools.
- ShellCheck (opens in a new tab)ToolFree
Paste a script or command and see unquoted variables and other classic traps flagged.
- The Linux Command Line (free book) (opens in a new tab)BookFree
A full book on navigation, permissions, processes, pipes and scripting.
More for this topic
- jq Manual (opens in a new tab)DocsFree
How to filter and count fields in JSON from the command line.