Typorithm · medium

grep

bash-grepbashgrepsearchregex
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn grep

0%

Text Processing

grep prints lines matching a pattern. -i ignores case, -r recurses into directories and -n adds line numbers, -c counts matching lines, -v inverts the match, and -E enables extended regular expressions.

grep scans its input line by line and prints those that match a pattern. grep "error" app.log shows every line of the log containing the word error.

Flags shape the search: -i ignores case so warn and WARN both hit, -r recurses through a directory tree, and -n prefixes each result with its line number — grep -rn "TODO" src/ is the standard way to find every TODO in a project.

Other flags change the output or logic: -c reports a count instead of the lines, -v inverts the match to show non-matching lines, and -E turns on extended regex for patterns like alternation and grouping.

01

grep prints lines matching a pattern.

02

-i ignores case; -r recurses; -n adds line numbers.

03

-c counts matches; -v inverts; -E enables extended regex.

grep "error" app.log
grep -i "warn" app.log
grep -rn "TODO" src/
grep -c "200" access.log
grep -v "debug" app.log

Read the implementation

Connect each part of the code to the concept before you type.

1. Match a pattern

grep prints every line of app.log that contains the pattern "error".

grep "error" app.log

2. Ignore case

-i makes the match case-insensitive, so WARN and warn both match.

grep -i "warn" app.log

3. Recurse with line numbers

-r searches src/ recursively and -n prefixes each match with its line number.

grep -rn "TODO" src/

4. Count matches

-c prints the number of matching lines instead of the lines themselves.

grep -c "200" access.log

5. Invert match

-v inverts the match, printing only lines that do NOT contain "debug".

grep -v "debug" app.log

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

grep: Lesson & Practice · Typorithm | ApplyDjinn