Quiz
Warm-up from lesson 3-1: which command prints an entire file to the terminal at once?
grep: find lines that match
Here is a week-one engineering task: a service misbehaves, the log file is 80,000 lines, and the one line you need contains the word "error". Scrolling is not a strategy. This is the problem grep was built for, and why it's one of the most-used commands in existence.
grep pattern file reads the file and prints only the lines containing the pattern. That's the whole idea.
grep milk shopping.txt
The flags you'll actually use:
-i: ignore case, somilkalso matchesMilkandMILK.-n: show line numbers next to each match.-v: invert, printing lines that do NOT match.-c: just count matching lines instead of printing them.
grep is case-sensitive by default. Beginners lose ten minutes to this at least once, so watch it happen safely in the example below.
Code exercise · bash
Run it. Plain grep misses "Milk shake" (capital M). Adding -i catches it.
Code exercise · bash
The other flags on a numbers file: -n shows where matches are, -c counts them, -v flips the filter. (The | head part trims -v's long output to 3 lines. The | pipe is unit 5's topic.)
Quiz
You want every line of config.txt that does NOT contain the word "disabled". Which command?
Code exercise · bash
Your turn. The starter builds a 4-line log. Print every error line regardless of case, then print how many there are. Expected output: ``` error: disk full ERROR: no network 2 ```