Using grep.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,477
Reaction score
3,209
Credits
31,144

Using grep in Linux​

The grep command in Linux is a powerful tool used for searching text using patterns. It stands for "Global Regular Expression Print" and is commonly used to search through files and output from other commands.

What is grep?​

grep is a command-line utility that searches through text files for lines that match a specified pattern. It can search for simple strings or complex regular expressions. The basic syntax of grep is:

Code:
 grep [options] pattern [file...]

Examples of Using grep from a Text File​

Let's say we have a text file named example.txt with the following content:

Code:
 Hello World This is a test file 
grep is a powerful tool Use grep to search text

To search for the word "grep" in example.txt, you can use the following command:

Code:
 grep "grep" example.txt

This will output:

Code:
 grep is a powerful tool Use grep to search text

Examples of Using grep from the Output of a Command​

You can also use grep to filter the output of other commands. For example, to search for the word "error" in the output of the dmesg command, you can use:

Code:
 dmesg | grep "error"

This will display all lines from the dmesg output that contain the word "error".

Avoiding the Use of cat with grep​

It's a common mistake to use cat with grep like this:

Code:
 cat example.txt | grep "grep"

However, this is unnecessary because grep can read files directly. The correct way to use grep is:

Code:
 grep "grep" example.txt

Using grep directly on the file is more efficient and avoids the overhead of using cat.

Using egrep or grep -E for Multiple Patterns​

egrep (or grep -E) allows you to search for multiple patterns using extended regular expressions. For example, to search for either "grep" or "test" in example.txt, you can use:

Code:
 egrep "grep|test" example.txt

Or equivalently:

Code:
 grep -E "grep|test" example.txt

Using grep -r for Recursive Search​

The -r option allows you to search recursively through directories. For example, to search for the word "grep" in all files within the current directory and its subdirectories, you can use:

Code:
 grep -r "grep" .

Using grep -v to Exclude a Pattern​

The -v option inverts the match, displaying all lines that do not contain the specified pattern. For example, to display all lines in example.txt that do not contain the word "grep", you can use:

Code:
 grep -v "grep" example.txt

Using grep -i for Case Insensitivity​

The -i option makes the search case-insensitive. For example, to search for the word "grep" in example.txt regardless of case, you can use:

Code:
 grep -i "grep" example.txt

Using grep -x for Exact Match​

The -x option matches only those lines that exactly match the whole pattern. For example, to search for lines that are exactly "Hello World" in example.txt, you can use:

Code:
 grep -x "Hello World" example.txt

Additional Suggestions​

  • Using grep -c to Count Matches: The -c option counts the number of matching lines. For example, to count how many lines contain the word "grep" in example.txt, you can use:
    Code:
     grep -c "grep" example.txt
  • Using grep -l to List File Names: The -l option lists the names of files with matching lines. For example, to find which files in the current directory contain the word "grep", you can use:
    Code:
     grep -l "grep" *
 
Last edited:


To look for mutiple strings.

Code:
 egrep "grep|test|Hello|tool" example.txt

Or equivalently:

Code:
 grep -E "grep|test|Hello|tool" example.txt
 
Don't forget grep -q "target" "filename" where you just want to know if target appears in the file but don't care to see it just now.

Code:
grep -q "ERROR" mylog.txt && echo "Well, you broke it!" && exit 10 || echo "No errors detected"

# ... or, using stdin instead of a file...
md5sum -C someisofile.md5.txt |grep -q "FAILED" && echo "bad download"
 

Using grep -m to Stop After a Certain Number of Matches​

The -m option allows you to stop searching after a specified number of matches. For example, to find the first 2 occurrences of the word "grep" in example.txt, you can use:

Code:
 grep -m 2 "grep" example.txt

Using grep -q for Quiet Mode​

The -q option makes grep operate in quiet mode, where it exits immediately with a status of 0 if a match is found, and 1 if no match is found. This is useful for scripts. For example, to check if the word "grep" exists in example.txt without displaying the output, you can use:

Code:
 grep -q "grep" example.txt

Using grep -n to Show Line Numbers​

The -n option displays the line numbers of matching lines. For example, to find the word "grep" in example.txt and show the line numbers, you can use:

Code:
 grep -n "grep" example.txt

This will output something like:

Code:
3:grep is a powerful tool
4:Use grep to search text

Using grep -A and grep -B to Include Lines Before and After the Match​

The -A option includes a specified number of lines after the matching line, and the -B option includes a specified number of lines before the matching line. For example, to find the word "grep" in example.txt and include 1 line before and 1 line after each match, you can use:

Code:
 grep -A 1 "grep" example.txt

This will output something like:

Code:
This is a test file grep is a powerful tool
Use it to search text

Using grep -U to Match Strings in a Binary File​

The -U option allows grep to search for patterns in binary files. For example, to search for the string "error" in a binary file named binaryfile.bin, you can use:

Code:
 grep -U "error" binaryfile.bin

This will display any lines in the binary file that contain the string "error".
 
Last edited:
Top