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...]
- pattern: The text or regular expression you want to search for.
- file: The file or files where you want to search for the pattern.
- If no file is provided,
grepwill read from standard input.
Common grep Options:
-i: Ignore case (case-insensitive search).-ror-R: Search recursively in directories.-l: Show only the names of files containing the pattern.-n: Show line numbers where the pattern appears.-v: Invert the match (show lines that do not match the pattern).-c: Count the number of lines that match the pattern.-H: Show filenames with matching lines.-o: Show only the matched parts of the line.-w: Match whole words only.-A <num>: Show<num>lines after the match.-B <num>: Show<num>lines before the match.-C <num>: Show<num>lines before and after the match.
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
- Explanation: This searches for the exact string
"hello"infile.txtand prints the matching lines.
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
- Explanation: This will match
hello,Hello, andHELLO.
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
- Explanation: The word
hellosorhello123will not be matched, but the wordhellowill be.
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
- Explanation: This shows the line numbers before each matching line.
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
- Explanation: This searches all files in the specified directory (and subdirectories) for the string
"hello".
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
- Explanation: This will show all lines that do not contain the word
"hello".
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
- Explanation: This shows only the exact matches for
"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
- Explanation: This counts the number of lines that contain the string
"hello".
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
- Explanation: This shows 2 lines after the matching line.
Example (show 2 lines before the match):
grep -B 2 "hello" file.txt
Sample Output:
goodbye world
see you later
hello world
- Explanation: This shows 2 lines before the matching line.
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
- Explanation: This shows 2 lines before and after the matching line.
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
- Explanation: This shows which file and line the pattern is found in for each file.
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
- Explanation: This uses a regular expression (
hello|world) to match either "hello" or "world".
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
- Explanation: This will search for
"hello"within the compressedfile.txt.gz.