Skip to main content
Bash and Linux

Find Lines with grep

easygrep and regex Must-do

Problem statement

Use grep to pull the lines you care about out of a log: every ERROR line with its line number, only whole-word matches, a count of warnings in any case, everything that is not INFO, and an exact piece of text that contains special characters. This is the first move in almost every incident: open the log, find the errors, see when they started.

app.log

TEXT
2026-10-01 08:00:01 INFO server started
2026-10-01 08:00:05 warn cache is cold
2026-10-01 08:01:10 ERROR db timeout after 30s
2026-10-01 08:01:11 INFO retry ok
2026-10-01 08:02:00 WARN disk 81% full
2026-10-01 08:02:30 ERRORS summary: 1
2026-10-01 08:03:00 ERROR payment failed [code=500]
2026-10-01 08:03:30 INFO shutting down

Print, in this order:

  1. Every line that contains ERROR, with its line number.
  2. The same, but only where ERROR is a whole word (so not ERRORS).
  3. How many lines mention a warning, in any case.
  4. Every line that is not an INFO line.
  5. How many lines match [code=500] with and without -F, then the one real match.

Expected output:

TEXT
== lines with ERROR, numbered ==
3:2026-10-01 08:01:10 ERROR db timeout after 30s
6:2026-10-01 08:02:30 ERRORS summary: 1
7:2026-10-01 08:03:00 ERROR payment failed [code=500]
== only the word ERROR ==
3:2026-10-01 08:01:10 ERROR db timeout after 30s
7:2026-10-01 08:03:00 ERROR payment failed [code=500]
== how many warnings, any case ==
2
== everything that is not INFO ==
2026-10-01 08:00:05 warn cache is cold
2026-10-01 08:01:10 ERROR db timeout after 30s
2026-10-01 08:02:00 WARN disk 81% full
2026-10-01 08:02:30 ERRORS summary: 1
2026-10-01 08:03:00 ERROR payment failed [code=500]
== search for the exact text [code=500] ==
without -F: 8 lines match
with -F: 1 line matches
2026-10-01 08:03:00 ERROR payment failed [code=500]

Hints

Hint 1: grep pattern file prints matching lines. One letter flags change what counts as a match (-i, -w, -F) or what gets printed (-n, -c, -v).

Approach

Optimal: grep and its flags

Covers: grep, -n, -w, -i, -c, -v, -F, quoting patterns, why -c counts lines.

What grep does. grep reads a file one line at a time. For each line it asks one question: "does this line contain the pattern?" If yes, it prints the line. If no, it skips it. That is all, and it is fast enough to scan gigabytes.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB F[("app.log")]:::blue --> G{{"grep checks
each line"}}:::purple G --> Y["contains the pattern
printed"]:::green G --> N["does not
skipped"]:::gray classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px

The flags you will use every day. They fall into two groups: flags that change what counts as a match, and flags that change what is printed.

Flag Changes Example
-i match: ignore upper and lower case grep -i warn finds warn and WARN
-w match: whole words only grep -w ERROR skips ERRORS
-F match: plain text, no special characters grep -F '[code=500]'
-v match: flip it, print lines that do not match grep -v INFO
-n output: put the line number in front 3:...
-c output: print only how many lines matched 2

Flags can be joined: grep -nw is the same as grep -n -w, and grep -ic counts in any case.

Why -w matters. A plain pattern matches anywhere inside a line, even inside a longer word. grep ERROR happily matches ERRORS, and grep 500 matches 15003. -w only accepts a match that has a non-word character (a space, punctuation, the start or end of the line) on both sides.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph PLAIN["grep ERROR"] direction TB P1["ERROR db timeout
matched"]:::green ~~~ P2["ERRORS summary
matched too"]:::yellow end subgraph WORD["grep -w ERROR"] direction TB W1["ERROR db timeout
matched"]:::green ~~~ W2["ERRORS summary
skipped"]:::gray end PLAIN ~~~ WORD classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style PLAIN fill:transparent,stroke:#d97706,stroke-width:2px style WORD fill:transparent,stroke:#059669,stroke-width:2px

Why -F matters. grep reads the pattern as a regular expression, a small language where some characters have special jobs. . means "any character", * means "repeat", and [...] means "any one of these". So [code=500] does not mean that text. It means "any one of the characters c, o, d, e, =, 5 or 0", and almost every line has one of those. That is why the count jumps to 8. -F (fixed strings) says "no special characters, find this exact text".

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart LR subgraph RE["grep '[code=500]'"] direction TB R1["any ONE of
c o d e = 5 0"]:::red --> R2["8 lines match"]:::red end subgraph FX["grep -F '[code=500]'"] direction TB F1["exactly the text
[code=500]"]:::green --> F2["1 line matches"]:::green end RE ~~~ FX classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px style RE fill:transparent,stroke:#dc2626,stroke-width:2px style FX fill:transparent,stroke:#059669,stroke-width:2px

Always quote the pattern. Put the pattern in single quotes, like grep 'disk 81%' app.log. Without quotes, the shell splits it at the space, so grep searches for disk in a file called 81%. Single quotes also stop the shell from touching *, $ and [.

Walking through the code. The # Setup: lines only create the sample log, so skip past them.

  1. grep -n 'ERROR' prints lines 3, 6 and 7. Line 6 is there because ERRORS contains ERROR.
  2. grep -nw 'ERROR' drops line 6, because in ERRORS the match is followed by the letter S.
  3. grep -ic 'warn' counts warn and WARN, which gives 2.
  4. grep -v 'INFO' prints the five lines that do not contain INFO.
  5. $(...) runs a command and puts its output into the echo line. Without -F, 8 lines match the bracket pattern; with -F, only the real one does.

Edge cases. If nothing matches, grep prints nothing and exits with code 1, and -c prints 0. On a missing file it prints an error and exits with 2. Scripts use those codes: if grep -q ERROR app.log; then ... runs only when there is at least one match, and -q keeps grep quiet.

# Setup: create the sample log in a fresh temporary folder
cd "$(mktemp -d)"
cat > app.log << 'LOG'
2026-10-01 08:00:01 INFO  server started
2026-10-01 08:00:05 warn  cache is cold
2026-10-01 08:01:10 ERROR db timeout after 30s
2026-10-01 08:01:11 INFO  retry ok
2026-10-01 08:02:00 WARN  disk 81% full
2026-10-01 08:02:30 ERRORS summary: 1
2026-10-01 08:03:00 ERROR payment failed [code=500]
2026-10-01 08:03:30 INFO  shutting down
LOG

echo "== lines with ERROR, numbered =="
grep -n 'ERROR' app.log

echo "== only the word ERROR =="
grep -nw 'ERROR' app.log

echo "== how many warnings, any case =="
grep -ic 'warn' app.log

echo "== everything that is not INFO =="
grep -v 'INFO' app.log

echo "== search for the exact text [code=500] =="
echo "without -F: $(grep -c '[code=500]' app.log) lines match"
echo "with -F:    $(grep -cF '[code=500]' app.log) line matches"
grep -F '[code=500]' app.log
RecapThe whole problem in a few lines, for the night before
  • Spot it: "show me the error lines", "how many warnings", "lines without X"
  • Idea: grep prints matching lines; -n numbers, -c counts, -v flips, -i any case, -w whole word, -F exact text
  • Cost: one pass over the file, O(n) time, almost no memory
  • Trap: -c counts lines, not matches; and . or [ ] in a pattern are special unless you use -F

Interview follow-ups

  • The log is gzipped, rotated, and 50 GB. What changes?

    Use zgrep, which reads .gz files directly, so zgrep -n ERROR app.log.3.gz works without unpacking. zgrep ERROR app.log* searches the current log and the rotated ones together. For a huge file, -F and LC_ALL=C give the biggest speed boost, and -m 20 stops after the first 20 matches when you only need a sample. grep reads one line at a time, so memory stays small whatever the file size.

Frequently asked questions

Both count matching lines, not matches. A line with three errors on it still counts once. If you need every match, print each one on its own line with -o and count those: grep -o 'ERROR' app.log | wc -l. Also check the case: grep -c error misses ERROR unless you add -i. For questions like "how many errors per minute", counting lines is usually what you want.

Use -F whenever you search for exact text that has punctuation in it: IP addresses (the dots are special), paths, JSON snippets, error codes in brackets. Without it, grep 10.0.0.1 also matches 10a0b0c1, because . means any character. -F is also faster, because grep does not need to run the pattern engine. On a very large log, LC_ALL=C grep -F 'text' is about as fast as grep gets.

The most common cause is case: grep error does not match ERROR, so try -i. The next is a special character in the pattern, like a . or [, so try -F. Windows line endings can also hide a match at the end of a line, because each line ends with an invisible \r; grep 'ok$' fails on ok\r. Finally, check that you searched the right file: logs are often split into app.log, app.log.1 and .gz files.