grep

grep stands for Global Regular Expression Print and is one of the most widely used commands in Unix-like systems to search for patterns in files. It allows you to search for specific strings or patterns in text, whether the text is from a file, output of another command, or a stream of data.


Basic Syntax:

grep [options] pattern [file...]

Common grep Options:


1. Basic Search in a File

You can search for a specific string in a file using grep.

Example:

grep "hello" file.txt

Sample Output:

hello world
hello again

2. Case-Insensitive Search

Use -i to make the search case-insensitive.

Example:

grep -i "hello" file.txt

Sample Output:

Hello world
hello again
HELLO there

3. Search for Whole Words Only

Use -w to match whole words only. This prevents partial matches (e.g., it won't match hello in hellos).

Example:

grep -w "hello" file.txt

Sample Output:

hello world

4. Display Line Numbers with Matches

Use -n to show the line numbers of matching lines.

Example:

grep -n "hello" file.txt

Sample Output:

1:hello world
3:hello again

5. Search Recursively in Directories

Use -r or -R to search files recursively in a directory.

Example:

grep -r "hello" /path/to/directory/

Sample Output:

/path/to/directory/file1.txt:1:hello world
/path/to/directory/file2.txt:3:hello again

6. Invert the Match (Show Non-Matching Lines)

Use -v to display lines that do not match the given pattern.

Example:

grep -v "hello" file.txt

Sample Output:

goodbye world
see you later

7. Show Only Matching Parts of the Line

Use -o to show only the matched parts of the line, not the entire line.

Example:

grep -o "hello" file.txt

Sample Output:

hello
hello

8. Count the Number of Matches

Use -c to count how many lines match the pattern.

Example:

grep -c "hello" file.txt

Sample Output:

2

9. Show Lines Before/After the Match

Use -A <num>-B <num>, or -C <num> to show lines before, after, or both before and after the match.

Example (show 2 lines after the match):

grep -A 2 "hello" file.txt

Sample Output:

hello world
how are you?
goodbye world

Example (show 2 lines before the match):

grep -B 2 "hello" file.txt

Sample Output:

goodbye world
see you later
hello world

Example (show 2 lines before and after the match):

grep -C 2 "hello" file.txt

Sample Output:

goodbye world
see you later
hello world
how are you?
goodbye world

10. Search for a Pattern in Multiple Files

You can search for a pattern in multiple files by specifying more than one file.

Example:

grep "hello" file1.txt file2.txt

Sample Output:

file1.txt:1:hello world
file2.txt:3:hello again

11. Search for a Regular Expression

grep can be used with regular expressions for more complex searches.

Example (search for lines containing either "hello" or "world"):

grep -E "hello|world" file.txt

Sample Output:

hello world
world of regex

12. Search for a Pattern in Compressed Files

You can search within compressed files like .gz using zgrep.

Example:

zgrep "hello" file.txt.gz

Sample Output:

hello world