Back to Blog
Arnošt Havelka

Searching Text with grep

Master grep to search for patterns in files.

Start Interactive Lesson
Searching Text with grep

Searching Text with grep

The grep command searches for patterns inside files. It's your detective tool for finding specific content.

Search for Text

Use grep 'pattern' file to find all lines containing that pattern.

Usage:grep'pattern'file
grep
Global Regular Expression Print - searches for text patterns in files.

Find matching lines:

Command Prompt
Buy milk Call dentist Buy groceries Remember meeting
C:\Users\User>cat notes.txt

Case-Insensitive Search: grep -i

The -i flag ignores uppercase/lowercase differences.

Usage:grep-i'pattern'file
-i
Ignore case - finds pattern regardless of capitalization.

Match any case:

Command Prompt
ERROR: file not found error: connection timeout Error handling complete
C:\Users\User>cat log.txt

Show Line Numbers: grep -n

The -n flag displays which line numbers contain the matches.

Usage:grep-n'pattern'file
-n
Line numbers - shows the line number of each match.

Locate by line number:

Command Prompt
host=localhost port=8080 host=192.168.1.1 debug=false
C:\Users\User>cat config.txt

Knowledge Check

1 / 3

What does grep do?

References

These Microsoft Learn and Windows documentation links provide authoritative details for the commands used in this article.

Up Next

Output Redirection

Learn to redirect command output to files.