Hello again, Linux learners! If you’re diving into Linux here's another tool to learn ... grep
.
Think of grep
as your superpower for searching for what you need. Whether you're sifting through logs, scripts, or directories, grep
helps you spot patterns.
Let’s break it down, as we train with some Marvel Superheroes!
📚 Table of Contents
- What is
grep
- Common
grep
Options - Scenario 1: Assembling the Avengers
- Scenario 2: S.H.I.E.L.D. Files
- Scenario 3: Finding TODOs in Stark Industries' Code
- Scenario 4: Multiverse Matchups
- Wrap-up
🤔 What is grep
?
grep
stands for Global Regular Expression Print. It’s used to search for text patterns within files and returns lines that match those patterns.
🧠 Basic Syntax:
grep [OPTIONS] PATTERN [FILE...]
⚙️ Common grep
Options
Option / Symbol | Description |
---|---|
-i |
Case-insensitive search |
-v |
Invert match (show lines not matching the pattern) |
-r or -R
|
Recursively search directories |
-n |
Show line numbers of matching lines |
-l |
Show filenames only (when a match is found) |
-c |
Count the number of matching lines |
-e |
Use multiple basic search patterns (can be used more than once) |
-E |
Use Extended Regular Expressions (ERE) |
--color |
Highlight matches in color |
^ |
Anchors the pattern to the start of a line |
$ |
Anchors the pattern to the end of a line |
🧪 Scenario 1: Assembling the Avengers (Basic Pattern Search)
Let’s create a simple text file of Marvel characters:
mkdir ~/marvel
cd ~/marvel
cat > heroes.txt <<EOF
Iron Man
Spider-Man
Black Widow
Thor
Hulk
Captain America
Doctor Strange
Ant-Man
Wasp
Black Panther
Scarlet Witch
Vision
Falcon
Winter Soldier
EOF
🎯Find all heroes with “man” in their names (case-insensitive)
grep -i "man" heroes.txt
🎯Exclude characters with “man” in their names (show everyone else)
grep -vi "man" heroes.txt
🎯Show line numbers of the matches
grep -in "man" heroes.txt
🧪 Scenario 2: S.H.I.E.L.D. Files (Using ^
and $
Anchors)
Let’s say you're working with S.H.I.E.L.D. files and want to extract specific entries:
cat > agents.txt <<EOF
Nick Fury
Maria Hill
Phil Coulson
Melinda May
Daisy Johnson
Grant Ward
Skye
Johnson
EOF
🎯Show names that start with “M”
grep "^M" agents.txt
🎯Show names that end with “son”
grep "son$" agents.txt
🎯Names that start with "D" and end with "n" (combine anchors)
grep "^D.*n$" agents.txt
🧪 Scenario 3: Finding TODOs in Stark Industries' Code
Let’s simulate a codebase with multiple files:
mkdir ~/marvel/project
cd ~/marvel/project
echo "# TODO: Upgrade arc reactor" > tony.sh
echo "# Fix suit alignment" > tony2.sh
echo "# TODO: Add nanotech support" >> tony2.sh
🎯Recursively search for all TODOs
grep -rn "TODO" ~/marvel/project
🎯Count all TODOs
grep -rc "TODO" ~/marvel/project
🧪 Scenario 4: Multiverse Matchups (Multiple Patterns)
Create a file of characters from across the multiverse:
cat > multiverse.txt <<EOF
Peter Parker
Miles Morales
Gwen Stacy
Stephen Strange
Loki
Sylvie
Wanda Maximoff
Tommy Maximoff
Billy Maximoff
Strange Supreme
EOF
🎯Find all characters with either “Strange” or “Maximoff”
grep -E "Strange|Maximoff" multiverse.txt
🎯Find characters whose names end in “e”
grep "e$" multiverse.txt
🏁 Wrap-up
grep
is like having the Eye of Agamotto at your fingertips, letting you bend text to your will. Learning it is useful for troubleshooting, scripting, and navigating any Linux system.
🚀 Your Mission:
- Create your own files using your own characters or plotlines.
- Practice filtering with
^
,$
,-i
,-v
,-n
, and-r
. - Try more advanced patterns using
-E
.
If this helped, give it a ❤️ or drop your favorite Marvel quote in the comments.
Until next time… grep assemble! 🛡️
💬 Let’s Connect
Top comments (0)