The Linux terminal can be a scary thing for many people, but the truth is that it is not. It is simple to use and has a power like no other. And that’s why it’s present in most of the world’s servers. On these servers there are many configuration files that we need to examine or search through. Well, that’s what the grep command in Linux is for, to help you search for character strings inside a text file.
The Grep command in Linux
According to the GNU website
“grep prints lines that contain a match for one or more patterns.”
Also, they add in the documentation:
“When it finds a match in a line, it copies the line to standard output (by default), or produces whatever other sort of output you have requested with options.”
Therefore grep has become a useful tool for sysadmin. It is also easy to use but quite powerful and flexible in its search criteria.
To complement the tutorial and make it easier to understand, common examples of the use of the grep command will be added.
How to use the grep command in Linux
The basic syntax of the command is as follows
grep [options] pattern [FILE]
Where grep is the command in question; Options refers to the possible options we can add; pattern is the string we want to search and finally we specify the file where we want to search.
If you want to get the help module of the command just execute it:
grep --help
Output:
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c
Pattern selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression
-F, --fixed-strings PATTERN is a set of newline-separated strings
-G, --basic-regexp PATTERN is a basic regular expression (default)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
Output control:
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
.
.
.
As you can see there are many options that give us grep, but the most important and common are
- -i: Of the most useful options because it allows us to ignore between capital and small letters.
- -c: It will only show us the number of lines that match with what we have asked to search.
- -r: This option enables the recursive option of the search.
- -n: print line number with output lines.
- -v: select non-matching lines.
So let’s start with the examples.
Using the grep command
Suppose we have a text file called file.txt and we want to search for the word config, to do so, run the following command:
grep config file.txt
It’s that simple. If the command does not return any output it is because it does not appear in the file. Also, if the file is in another location, you have to define the absolute path.
And to perform the search ignoring upper and lower case, just add the option -i
grep -i config file.txt
Now if we want to know how many times the word we want to search is found, we only need to use the -c option
grep -c config file.txt
And, what if I want to search for several words? Well, we can do it with the help of a pipe. In this case we will search the file for the words config and word
grep config file.txt | grep word file.txt
It’s that simple.
But you can also search for a word in several files:
grep config file1.txt file2.txt
But you can also combine the grep command with others to help with the results.
For example, you can use it with ls and search for a folder or file with the search criteria.
ls -l | grep img
This will display all files and folders that you start or that contain images. This same formula can be used with any command.
Another feature that we can take advantage of grep is to invert the result, for this there is the -v option:
grep -v config file.txt
You can also highlight the highlight, to do so add –color to the
grep config file.txt --color
So grep is quite useful.
Conclusion
There are quite useful commands that can help with the heavy lifting in Linux. One of them is grep because it allows you to search inside a text file or even combine it with another command to perform a unique filtering. This is appreciated by sysadmin and all those who work with configuration files and the terminal on a recurring basis
Now we want to hear from you. Have you used grep? are you still using it? leave us a comment and share this post with your friends.