grep
is one of the most frequently used commands in Linux.
For server developers, grep
is essential when searching and analyzing logs or data.
grep Options
Using grep
as-is is fine, but there are some options that can make it more useful.
Knowing grep
and being able to use it efficiently are very different.
Creating a File to Test grep options
if __name__ == "__main__":
with open("numbers.txt", 'w') as fw:
for i in range(10000):
fw.write("numbers: " + str(i) + " ...\n")
Before learning the options, let’s create a test file for grep
.
The file will contain numbers from 0 to 9999 for testing.
If something is unclear, working with a test file can make it easier to understand.
Ignoring Case Sensitivity
grep Num numbers.txt
grep -i Num numbers.txt
grep -i
ignores case when searching.
Limiting the Number of Matches
grep -m 5 1234 numbers.txt
grep -m 1
prints only the first 1 line (max count) that matches.
Displaying Line Numbers
grep -n 1234 numbers.txt
grep -n
includes the line number where the match is found.
Displaying Non-Matching Lines
grep -v 1 numbers.txt
grep -v
outputs only the lines that don’t match (vinvert match).
Restricting Output to a Specific Range
grep -o 1234 numbers.txt
grep -o ".\{0,3\}1234.\{0,3\}" numbers.txt
grep -o
prints only the matching part of the line (only match).
You can use regular expressions with -o
to show only the surrounding context of the match.
This is especially useful for long lines where you need to extract specific parts.
OR Condition
grep -e 1234 -e 5678 numbers.txt
grep -e
allows searching using multiple regular expressions, working like an OR condition.
Displaying Lines Before and After Matches
grep -A 2 1234 numbers.txt
grep -B 2 1234 numbers.txt
grep -C 2 1234 numbers.txt
grep -A 2
: Displays 2 lines After the match.grep -B 2
: Displays 2 lines Before the match.grep -C 2
: Displays 2 lines before and after the match.
Displaying File Names
grep -H 1234 numbers.txt
grep -h 1234 numbers*
grep -H
: Includes the file name in the output.grep -h
: Omits the file name from the output.
By default, the file name is shown when searching multiple files but omitted when searching a single file.
Recursive Search
grep -r 1234 .
grep -r 1234 .
searches recursively in the current directory.
Counting Matches
grep -c 1234 numbers.txt
grep -c
displays the count of matching lines.
Advanced grep Usage
Using just grep
might make you a beginner in command-line usage.
Performance depends on how efficiently you combine grep
with other commands.
Basic Usage
grep 1234 numbers.txt
grep 1234 numbers.txt*
This is the simplest and most commonly used way to use grep
.
Using with Pipeline
cat numbers.txt | grep 1234
cat numbers.txt | grep 1234 | cut -d ':' -f 2
You can use grep
more efficiently by combining it with other commands via pipelines.
Using with tail
tail -f numbers.txt | grep 1234
tail -F numbers.txt | grep 1234
tail -f
and tail -F
are used to follow the lines added to a file in real-time.
These are commonly used to analyze server logs as they are written.
However, it’s nearly impossible to monitor thousands of lines per second.
By combining tail and grep, you can analyze only the desired logs in real-time.
tail -f
Follows the current file.
tail -F
Follows based on the file name, even if the file is replaced.
Logs often roll over to a new file based on time.
With -f, if the file name changes due to rolling, no further logs will be shown.
-F, on the other hand, continues following logs even after the file name changes.