1 Minute to Learn Grep Command in Linux with Examples
Grep command is one of the best common ones for Unix users. If you use it well, you can improve your productivity using Linux. In this article, I will help you learn grep command in linux with simple examples. I believe that you can be a grep expert after reading this post.
What is Grep command in Linux?
Grep is a command-line tool for looking for lines that match a regular expression in plain-text data sets. The text search pattern is called a regular expression. When it discovers a match, it generates lines contain the result to Terminal. It is an acronym that stands for Global Regular Expression Print. Nowadays, use can use grep without any installation on some Unix-like systems such as Ubuntu, Zorin OS, Fedora, Arch Linux and so on.
Learn Grep command with examples
I created a file named grep-linux.txt
to help you learn grep in Linux. You can create one and follow me.
THIS IS THE 1ST LINE IN THIS FILE.
This is the 2nd line in this file.
Phone: 0123456789
Email: [email protected]
1. Search for the string in a file
Its basic function is to look for a given string in a specified file, as demonstrated below. To search a string, you can use this command
grep "pattern" "file"
The above command displays all instances that contain the word li
in the file and outputs the results.
2. Using with ignoring case-sensitivity
In the above example, you learn grep by filtering the word li
in the file, but it only return 2nd line because this line contains exactly line
(lowercase). You can also grep with ignoring case sensitivity by following this command
grep -i "pattern" file
We use option -i
or --ignore-case
to filter words that ignore case sensitivity
3. Finding whole words only
If you follow the first example to find a word that contains the word is
, the result will be line 2 with three matches: This
, is
, this
. To find whole words only, we will use option -w
or --word-regexp
.
grep -w "pattern" file
This image will help you visualize it better.
4. Learn Grep with Regex
As we know, grep is stands for Global Regex Expression Print, so that you can use Regex to find patterns in files. Let consider this command to find a line that starts with character P
(in Phone
word)
grep regex_pattern file
grep ^P file
5. Counting the number of matches
Option -c
stands for count
. It helps you to display the number of lines where each line has letters matching the pattern. To use this command, you can use:
grep -c pattern file
In the first command, it returns 1
because there is only line 2 contains the is
word. Same as in the 2nd statement, we have 2nd and 5th lines containing the word matching pattern.
6. Find files containing the extension
In Linux, learn grep also allows us to search for files that match the patterns, let’s look at the example of finding files with the extension webp
.
ls Downloads | grep ".webp"
You can replace webp
with any other extension to locate specific files. You can also use regex
in the above command.
7. Grep all Files in Directory
To search files in the current directory (or any folder) that contain pattern, we can use an asterisk instead of a filename or location of the folder we want to grep. We can also use option -r
to use recursive search.
grep -r pattern *
grep -r pattern ~/Downloads
8. Find phone number by Grep
You can use regex to filter phone numbers from text files. Note that you must adjust the template to fit the type of phone number you need.
grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?[0-9]\{3\}[ -]\?[0-9]\{4\}' file
9. Learn Grep to find email address
Same as finding phone numbers, you can also use this command to find email addresses in Linux. To do it, you should run this command:
grep -E '\b[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b' file
10. Show lines around result
When working with large files, having the option to select a search that displays the lines before, after, or around the result line can be useful. Using this grep’s option to do it:
grep -<A, B or C> <n> "pattern" file
-- A : after
-- B : before
-- C : around
-- n : count lines
Conclusion
After reading this article, I hope you can use this command after I help you learn grep. If you are needed, please leave a comment below, I will try my best to help you.