Lesson 12 of 17

Searching Text

Finding Patterns in Files

grep (global regular expression print) searches for lines that match a pattern and prints them.

Basic Usage

grep "pattern" filename

Example:

grep "Linux" notes.txt

If notes.txt contains:

Learn Linux
Practice daily
Have fun

Output:

Learn Linux

Only lines containing "Linux" are printed.

Common Flags

FlagMeaning
-iCase-insensitive search
-vInvert match — print lines that do NOT match
-nShow line numbers
-rRecursive — search all files in a directory
-lShow only filenames that contain matches

Examples

Search case-insensitively:

grep -i "linux" notes.txt

Print lines that do NOT contain "Linux":

grep -v "Linux" notes.txt

Output:

Practice daily
Have fun

Regular Expressions

The "pattern" can be a regular expression:

grep "^P" notes.txt    # lines starting with P
grep "y$" notes.txt    # lines ending with y

Your Task

Search notes.txt for lines containing Linux.

Linux shell loading...
Loading...
Click "Run" to execute your code.