In the Linux world, General Regular Expression Parser or grep is one of the most powerful commands used by every Linux administrator. Using it, you should be able to search files content and also perform a search within the command output: not just that, but a lot more, which we will be discussing in this tutorial.
The GREP command usage with examples
In this guide, we will walk you through the command usage and show you some helpful and useful examples that should save a lot of time. This tutorial is performed on the Ubuntu machine, but the command works on all Linux distributions.
Installation
To start using the command, you need first to check if it is already installed on your system. If not installed, run the following command in the terminal.
sudo apt install grep
You can check the command version to ensure that it is installed successfully.
grep --version
Syntax
grep [option/s] [pattern] [file]
Where the [option/s] can be:
- -i –> used to ensure the search pattern regardless of its case sensitivity.
- -c –> Preview, the count of the matching pattern.
- -v –> select non-matching lines that do not contain the given pattern.
- -n –> Preview the line number.
Where the [pattern] is a regular expression, and the [file] is the file you are going to search in its content.
Examples
We shall create a test file to use in our case scenario examples.
vi grep_tuts
Paste the following lines in the grep_tuts file:
Hi Guys, i am hend. And this is a tutorial file for the usage of the grep commad. The next 2 lines are empty lines. Hope You Will Enjoy This Tutorial. Bye.
Example 1. To search for a string in a single file.
grep "hend" grep_tuts
Example 2. Let’s make a copy from the grep_tuts file, which will use later on.
cp grep_tuts grep_tuts2
Now to search for a specific string in multiple files, use the next command.
grep "hend" grep_tuts grep_tuts2
Example 3. To search for a certain string in a file regardless of its case.
grep -i "tutorial" grep_tuts
Example 4. To search for a certain pattern in a file regardless of case sensitivity.
grep -i "the.*lines" grep_tuts
This means to search for any lines start with “the” string and ends with “lines.” Where the * denotes zero or more characters, and the -i option is to ignore “the” and “lines” case.
But in case you did not use the -i option, there will be no result.
grep "the.*lines" grep_tuts
Example 5. To display two lines after the pattern you are searching for.
grep -A 2 "hend" grep_tuts
Example 6. To display one line before the pattern you are searching for.
grep -B 1 "hend" grep_tuts
Example 7. To display one line around (or after and before) the pattern you are searching for.
grep -C 1 "hend" grep_tuts
Example 8. To search for a certain string in all files that exist in the current directory.
grep -r "Enjoy" *
Example 9. To display all the lines that do not contain the given string.
grep -v -i -e "Enjoy" grep_tuts
Example 10. To display the count of all lines that contain the string you are searching for.
grep -c "this" grep_tuts
Example 11. To display the count of all lines that contain the string you are searching for regardless of case sensitivity.
grep -c -i "this" grep_tuts
Example 12. To display the count of the un-matching pattern.
grep -v -c "this" grep_tuts
Example 13. To display all file names that contain the string you are searching for.
grep -l "this" *
Example 14. To display the number of lines.
grep -n "this" grep_tuts
Example 15. To search the output of a specific command. For example, list all the running processes that are started by the “tuts” user.
ps aux | grep tuts
That’s all about the usage of grep command in Linux.
Hello,
Sometimes, when you need to filter out the process list, you end up with an extra line you may not want to see.
$ps ax | grep kworker
23421 ? I 0:00 [kworker/u8:1-events_unbound]
25365 ? I 0:00 [kworker/u8:0-events_unbound]
31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
31490 ? I 0:00 [kworker/2:0-events]
31816 ? I 0:00 [kworker/1:1-events]
31983 ? I 0:00 [kworker/0:3-events]
355 pts/0 S+ 0:00 grep –color=auto kworker
This is because the grep command contains the word that match. If you don’t want to see it, then use this :
$ps ax | grep [k]worker
23421 ? I 0:00 [kworker/u8:1-events_unbound]
25365 ? I 0:00 [kworker/u8:0-events_unbound]
31422 ? I 0:00 [kworker/3:2-cgroup_destroy]
31490 ? I 0:00 [kworker/2:0-events]
31816 ? I 0:00 [kworker/1:1-events]
31983 ? I 0:00 [kworker/0:3-events]
The grep line has gone! How’s that?
A+
(My 2 cents)