Grep Command with Examples

Grep stands for (Global Regular Expression Print) is a Linux command-line utility to perform essential regular expression in the file.

Finding strings and patterns from the file and streaming the output type of operation can be performed using the grep command.

Grep can be piped with other commands. For example, you can pipe cat and grep together to search strings from files and display them on the screen.

Today, you will learn to use the grep command with advanced examples.

Searching for String

To perform the search operation, I have created a sample file with the name “file.txt”, below is the content inside that file.

Hello TRENDOCEANS! Reader
Hope you doing great
We have generated this file to demonstrate
the working of grep command
If you have any question or quarries comment down.

You can see a few keywords example, “you” has been repeated multiple times from above. You can use the “-i” option to perform the search operation on this file to find all the statements containing the “you” keyword, as shown below.

$ grep -i "you" file.txt
Hope you doing great
If you have any question or quarries comment down.

Two statements came in the output containing the “you” keyword in them.

Note that when grep, by default perform case insensitive search operation. If you type small “you” or capital “YOU” both are equally handled by grep command, as shown below.

$ grep -i "YOU" file.txt 
Hope you doing great
If you have any question or quarries comment down.

This same operation can be done by piping the grep command with the cat command, as shown below.

$ cat file.txt | grep you
Hope you doing great
If you have any question or quarries comment down.

Count the Number of Matches

We can also output the number of times they are repeated, as shown below, by extracting matched strings.

$ grep -c "you" file.txt 
2

In this case, grep considers case-sensitive, meaning the string “you” and “YOU” will be treated differently.

Output the Files Containing Matches

Do you have multiple files containing some specific string? You can output the files which share the common string using the below command.

[email protected]:~$ grep -l "you" *
grep: Desktop: Is a directory
grep: Documents: Is a directory
grep: Downloads: Is a directory
file1.txt
file2.txt
grep: Music: Is a directory
grep: Pictures: Is a directory
grep: Public: Is a directory
grep: Templates: Is a directory
grep: Videos: Is a directory

Above, you can see I have replicated “file.txt” content to “file1.txt” and “file2.txt” and will perform searching them based on “you” keywords to do that “-l” option is being used. If you notice, instead of my file containing matches, grep is also trying to find it in the nested directory.

We were using the “*” symbol while executing the command. If you are sure about your files containing the same file extension, then replace “*” with “*.txt” it will only search for files with the txt extension, as shown below.

$ grep -l "you" *.txt
file1.txt
file2.txt

I recommended approaching this method instead of searching matches recursively in the nested directory, which also strains your system memory.

Show Matches Line Number

Finding matches is helpful, but you have to edit and find that line to modify that string after that. In grep, you can use “-n” option to display a line with respect to matched strings, as shown below.

$ grep -n "you" file.txt 
2:Hope you doing great
5:If you have any question or quarries comment down.

Inverting the Pattern Match

If you are focusing on sentences that do not contain the specific keyword and output them, you can perform that operation with the help of the “-v” option.

This helps list all the sentences that do not contain the matches, as shown below.

$ grep -v "you" file.txt 
Hello TRENDOCEANS! Reader
We have generated this file to demonstrate
the working of grep command

Conclusion

Grep is a handy and helpful command able to find a single string from a thousand words with the help of regular expression.

Leave a Reply