LAB-TEXT-02 - The Surgeon's Knife: grep

Use grep to find matching lines, ignore noise, and search recursively through realistic text samples.

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
What success looks like
  • 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
  • Work on copies of text data until you are confident that filters and substitutions behave as expected.

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

Common grep patterns

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

  1. Create a workspace:
mkdir -p "$HOME/grep_lab"
cd "$HOME/grep_lab"
  1. Create sample log content:
printf "INFO boot\nWARN disk almost full\nERROR service stopped\nINFO done\n" > app.log
  1. Search for one word:
grep "WARN" app.log

Exercise G2: Ignore Case

  1. Add one lowercase warning:
echo "warning cache cold" >> app.log
  1. Compare these two commands:
grep "warning" app.log
grep -i "warning" app.log
  1. Confirm why -i matters.

Exercise G3: Remove Comment Lines

  1. Create a small config file:
printf "# comment\nPort 22\n# disabled\nPermitRootLogin no\n" > config.conf
  1. Remove comment lines:
grep -v "^#" config.conf
  1. Confirm that only the active-looking settings remain.
S
Solo Task described, hints available - figure it out
>

Exercise S1: Search a Folder Recursively

  1. Create a subfolder:
mkdir -p configs
cp config.conf configs/ssh.conf
echo "timezone=UTC" > configs/app.conf
  1. Search the folder:
grep -r "timezone" configs
  1. Confirm that grep shows both the filename and the matching line.

Exercise S2: Use grep in a Pipe

  1. Run:
cat app.log | grep "INFO"
  1. 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:

  1. lines starting with #
  2. lines that are completely blank

You should end up with only the active settings shown on screen.

What to do next

Best next step Learning

Return to the related lesson if you want the concept explained more deeply.

Open now