Global regular expression printing (grep) is a powerful tool that searches a text file or standard input for lines matching a regular expression and prints the results to standard output. A regular expression (regex) is a special sequence of characters that helps you efficiently match strings in text or files.
In UNIX/Linux, the grep command is often used with regular expressions to find matching lines in a file. The general syntax of the grep command is as follows:
grep [options] pattern [file...]
Among them, pattern is the regular expression pattern to be matched, and file is the file name or file path to be searched. The file parameter can be omitted, in which case grep will read data from standard input.
The grep command will output all matching lines, and the matching parts of the lines will be highlighted. Different options can be used to control the behavior of grep, such as using the -i option for case-ignoring matching, the -r option for recursive searches, etc.
Basic regular expression characters support the following:
. | Match any single character in the string |
[ ] | Matches any character within brackets or a range of characters and numbers |
^ | Matches the first character of the string |
$ | Match the last character of the string |
Extended regular expressions support basic regular expressions and some additional characters:
{n} | Match the previous character exactly n times |
{n, m} | Match the previous element at least n times and no more than m times |
? | Match the above characters One or zero times |
+ | Match the above character one or more times |
| | Match any string option, such as “abc”|”def” – “abc” or “def” |
* | Matches the preceding element zero or more times. |
To skip any of these characters and treat them as string literals, place a backslash before them. For example, if you do not want to be treated as a regular expression character, but only as a question mark, use . This works for all regular expressions. ?
\?
Grep supports basic regular expressions by default and extended regular expressions with options. If you prefer, you can use it by itself, called . -E
grep -E
egrep
In this tutorial, you will learn how to use grep with strings, basic regular expressions, and extended regular expressions.
grep syntax
grep [options] 'expression' text
[options] for example or –, we will explore them later. -i
E
Expression represents a search pattern, which can be a string literal or a regular expression.
Text represents standard input, which can be a file, multiple files, or the output of other commands.
I have prepared a list of movies in a file called movies.txt We will use this text file as input to grep and search for a specific search pattern in this file.
Top movies of all time: The Shawshank Redemption (1994) - 9.2 The Godfather (1972) - 9.2 The Dark Knight (2008) - 9.0 the godfather part II (1974) - 9.0 Angry Men (1957) - 8.9 City of God (2002) - 8.6
Basic search
See if The Godfather is in the movie.txt
grep 'Godfather' movies.txt
output
Grep returns rows that contain the search term “Godfather.”
Using grep with regular expressions
Example 1: Search for videos preceded by “The”.
grep '^The' movies.txt
Output:
As we all know, symbols in regular expressions match lines starting with the previous characters. ^
Example 2: Search for movies released after 2000:
grep '20[0-9][0-9]' movies.txt
Output:
Movies after 2000 can be represented as 20xx, where each x is any number between 0 and 9, so replaced in the expression by [0-9].
What is egrep?
As mentioned before, is another command that allows the use of extended regular expressions. Let’s see an example egrep
grep -E
Find movies with the word “The Godfather” or “Dark”
We know that OR in extended regular expressions is represented by . |
grep -E 'Godfather|Dark' movies.txt
output
If we use grep alone (without the -E option), the above command will not return anything because it treats the symbols as literals and not special characters. |
Additionally, the above command is equivalent to the following command:
egrep 'Godfather|Dark' movies.txt
Ignore case sensitivity
If you notice in the movies.txt file we also have the movie “The Godfather Part II” with lowercase letters, but none of our searches using the search term “The Godfather” returned this line. This is because grep is case sensitive and we can ignore case sensitivity using option -i
grep -i 'Godfather' movies.txt
output
We can see that regardless of case, the two lines with the word “godfather” are returned.
Search for complete words
Let’s see what happens if we search for the word “God”
grep 'God' movies.txt
output
Two movies with the keywords “The Godfather” and “God” are matched. This is because both keywords have the letters “God” in them.
To match only “god” (complete word) you can use the -w option
grep -w 'God' movies.txt
output
Return only matching words
We can use the option to return only the searched word instead of the entire line. -o
grep -o 'Godfather' movies.txt
Output:
Reverse search
Inverting the search returns everything except “expression”. This is the opposite of a normal search.
For example, let’s return all movies without the word “God”
grep -v 'God' movies.txt
output
We can see all the movies without the word “God”. Yet, we still see “The Godfather” there. This is because we are not ignoring case sensitivity in our search terms. We can use -i option with -v as shown below
grep -iv 'God' movies.txt
output
Return results with line numbers
Finding the line number can be useful if you want to edit a line that matches “expression”
To do this, use the -n option
grep -n 'Angry Men' movies.txt
output
We have it. The word “angry man” appears in line 7
We can edit that particular line using an editor like vim
vim + 7 movies.txt
It will take you directly to line 7.
Calculate the number of matching words
If we want to know how many movies have the word “God” we can use the -c option
grep -c 'God' movies.txt
output
Likewise, we can combine the options to see how many movies have “God” or “God” in their titles. -c
-i
grep -ic 'god' movies.txt
output
Return exact match (full line)
To find out whether a search term (expression) matches a row, use the -x option.
grep -x 'City of God (2002) - 8.6' movies.txt
It will only match if the entire line matches the searched term:
Returns file names with search terms
To see which files have our search term (expression) we use the -l option. We can pass multiple filenames to it or use asterisk (*).
grep -l 'Godfather' movies.txt grep.sh
or
grep -l 'Godfather' ./*
Here we tell grep to look for “godfather” in any file in the current directory.
Output:
./grep.sh ./movies.txt
Using option alone will only search the current directory, if there are subdirectories in the current directory, grep will not search them and throw an error. That’s because grep requires a file. To overcome this problem, we incorporate the option to find any file recursively. -l
-l
-r
grep -lr 'Godfather' ./grep
A dot (.) represents the current directory in Linux.
Output:
Return the file name and matching lines
This is like an option with extra functionality to return what the matching lines in the file look like. -l
We use the -H option:
grep -Hr 'Godfather' ./grep
Output:
As you can see, the -H option returns the filename along with the matching lines, separated by a colon (:).
Return to other rows after the game is over
Sometimes we want to know what rows follow the matching row. We can use -A option like below.
To print 2 more lines after the matching line:
grep -A2 'Dark Knight' movies.txt
Output:
So not only do we have the row that matches the expression “Dark Knight”, but we also have the 2 rows that follow.
Pre-match return line
This is like the -A option, but it works in the opposite direction. We use -B (before) to achieve this.
To get 2 rows before the row with matching expression “Dark Knight”:
grep -B2 'Dark Knight' movies.txt
Output:
Return lines before and after the game
This combines the -A and -B options. We use -C to achieve this.
To return 2 rows before and 2 rows after the row with the term “Dark Knight”:
grep -C2 'Dark Knight' movies.txt
Output:
Use grep to search the output of other commands
We can use Linux pipes to pass the output of any command as standard input to grep and search it in the same way as a file.
For example, let’s search the output of a command. ls
List directories only:
ls -l | grep '^d'
From the regex, we know this means matching any line in the output that starts with the letter “d”, which means a directory in Linux. ^
Output:
Conclusion
grep is a very powerful and useful command in UNIX systems. If used properly, it can save us valuable time. Regular expressions can be used to search files, multiple files, text patterns in directories, and the output of other commands very effectively and efficiently. In this tutorial, we explain how to use grep with regular expressions and demonstrate some of the most useful grep options.
Welcome to follow
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 15501 people are learning the system