LAB-TEXT-02 - The Surgeon's Knife: grep
TXT Text Processing
The Surgeon's Knife: grep
Use grep to find matching lines, ignore noise, and search recursively through realistic text samples.
35 min BEGINNER LINUX Curriculum-reviewed
Success criteria
- Use grep to find matching lines, ignore noise, and search recursively through realistic text samples.
- Repeat the workflow without copy-paste or step-by-step prompting.
Safety notes
- Practice recursive searches in known folders first so you understand the scope before searching larger trees.
Part A: The Field Guide
What This Lab Is Really About
grep is one of the simplest and most useful ways to turn a large block of text into a short answer.
You will use it to:
- find matching lines
- ignore case when needed
- remove lines you do not want
- search through multiple files
Command Reference
grep “WARN” app.log grep -i “warning” app.log grep -v ”^#” config.conf grep -r “timezone” configs/
Part B: The Drill Deck
Terminal required: work inside a disposable text-practice folder.
G Guided Step by step - type exactly this and compare the result >
Exercise G1: Create a Small Log File
- Create a workspace:
mkdir -p "$HOME/grep_lab"
cd "$HOME/grep_lab"- Create sample log content:
printf "INFO boot\nWARN disk almost full\nERROR service stopped\nINFO done\n" > app.log- Search for one word:
grep "WARN" app.logExercise G2: Ignore Case
- Add one lowercase warning:
echo "warning cache cold" >> app.log- Compare these two commands:
grep "warning" app.log
grep -i "warning" app.log- Confirm why
-imatters.
Exercise G3: Remove Comment Lines
- Create a small config file:
printf "# comment\nPort 22\n# disabled\nPermitRootLogin no\n" > config.conf- Remove comment lines:
grep -v "^#" config.conf- Confirm that only the active-looking settings remain.
S Solo Task described, hints available - figure it out >
Exercise S1: Search a Folder Recursively
- Create a subfolder:
mkdir -p configs
cp config.conf configs/ssh.conf
echo "timezone=UTC" > configs/app.conf- Search the folder:
grep -r "timezone" configs- Confirm that grep shows both the filename and the matching line.
Exercise S2: Use grep in a Pipe
- Run:
cat app.log | grep "INFO"- Confirm that grep works as both a direct file search and a pipeline filter.
M Mission Real scenario - no hints, combine multiple skills >
Mission M1: Show Only the Active Settings
Create a file named server.conf with this content:
# sample server config
Listen 8080
# Debug true
Mode production
Now write a pipeline that removes:
- lines starting with
# - lines that are completely blank
You should end up with only the active settings shown on screen.