grep command in Linux
This tutorial demonstrates the use of grep
the command to match a case-insensitive pattern, count the number of matches, display the matched file names, match the exact pattern, display only the matched pattern, display line numbers, reverse pattern matching, match the start of a string, and match the end of a line.
grep
Commands
in Linux
grep
Command stands for Global Regular Expression Print. grep
Command searches for a particular pattern in a given file.
Once a match is found, grep
the command displays all lines that match the pattern. grep
The pattern the command searches for is a regular expression.
grep
The command uses the following syntax.
grep [options] pattern [file]
grep
Use command to match case insensitive pattern
in Linux
grep
command can match the pattern without case sensitivity. We use the command with -r -i
option grep
to match the pattern ignoring case sensitivity.
-i
The -p option tells grep
the command to ignore case differences between the pattern and the data in the file.
The following image demonstrates the use of grep
command to match a pattern ignoring the case difference between pattern and data. We are currently in foo
the directory, which has delft.txt
file .
Use cat
the command to delft.txt
print the contents of the file to standard output. We use the command with -i
the option grep
to match delft.txt
the pattern in the file Havard
.
The output shows that two matches of delft.txt
are found in . There are different cases for the matching pair patterns in the file.Havard
delft.txt
This is because the -p option grep
in the command -i
tells grep
the command to ignore case differences between the pattern and the data in the file.
grep
Count the number of matches in Linux
using command
grep
The command can also count the number of correct matches. The command with -c
the -p option grep
prints the count of correct matches.
In the following image, we use the -p command with -i
the -d option grep
to check delft.txt
the pattern in Havard
. The command finds two correct matches.
We then use the same command, but this time, we include -c
the -p option to print the number of correct matches, and the command displays the number 2
.
This means that there are two correct matches for delft.txt
the pattern in the file .Havard
Use command in Linux grep
to provide matching file name
grep
The command can give the file names that contain a match. Using the command with -l
the option grep
will display the file names that contain a match with the pattern.
We illustrate how to use grep
the command to return the names of files that match a pattern. We foo
are working in the directory, which has two files, delft.txt
and example.txt
.
Use cat
the command to display the contents of the two files to standard output.
We use the command with -l
the -p option grep
and set the pattern to text
, passing in a wildcard *
as grep
the last argument to the command.
-l
The -p option tells grep
the command to display only the names of the files that contain a match, rather than the lines within the files.
The wildcard character *
tells grep
the command to search all files in the current directory.
grep
The command prints the file names example.txt
as a pattern match is found in this file.
grep
Use command to match exact pattern
in Linux
We can also use grep
the -p command to match an exact pattern. To do this, we use the -p command with -w
the -p option grep
.
The following image shows the use of grep
the command to match an exact pattern. We have a delft.txt
text file called . We use cat
the command to delft.txt
display the contents of the file to standard output.
Use the command with -w
the -p option grep
to match an exact pattern delft.txt
in a file Havard
. -w
The -p option tells grep
the command to match the entire pattern.
This option respects case differences between the pattern and the data in the file.
grep
The command displays lines with the exact pattern to standard output.
Use command in Linux grep
to display only matching patterns
Using grep
the -p command, we can display only the portion of the line that contains a match. We use the -p command with -o
the -p option grep
.
We are foo
in the directory, which contains the file in the image below delft.txt
. We use cat
the command to display the contents of the file to standard output.
Use the -o
-p command with the -p option grep
to match delft.txt
patterns in a file Yale
. Pattern matching takes into account case differences between the pattern and the data in the file. -o
The -p option tells grep
the -p command to print only the matching portion of the line.
The output display grep
command displays only the portion that matches the pattern.
grep
Use command to display line number
in Linux
Using grep
the -p command, we can also display the line numbers that contain pattern matches. We can use the -p command with the -p -n
option grep
to display the line numbers that contain pattern matches.
In the image below, we foo
are working in the directory.
foo
The directory contains a delft.txt
file called . We use cat
the command to delft.txt
print the contents of to standard output.
Use the -p command with the -p -i
and -n
-p options grep
to match delft.txt
patterns in a file Yale
. The -i
-p grep
command ignores the case differences between the pattern and the data in the file. -n
The -p option tells grep
the -p command to print out the line numbers of the matches.
From the output, we can see that line numbers have been printed for the lines that contain matches.
grep
Invert Pattern Matching
in Linux Using Command
Using grep
the -p command, we can display all the files that do not contain lines matching a pattern. We can use the -p command with -v
the -p option grep
.
-v
The -p option tells grep
the command to display only those lines that do not match the supplied pattern.
In the image below, we foo
are working in the directory.
foo
The directory has delft.txt
the file. We use cat
the command to delft.txt
display the contents of the file to standard output.
We use the command with -v
the -p option grep
to display all the lines that do not match the -p pattern delft.txt
in the -p file . The output displays all the lines that do not contain the -p pattern.MIT
MIT
grep
Use command to match the starting string
in Linux
We can use grep
the command to display all the lines in a file that start with a specific string pattern. We can ^
do this by using a regular expression pattern.
^
Specifies that the match should be ^
lines starting with the preceding pattern.
In the following image, we have grep
specified the match for the command as ^J
. This means that grep
the command should look for lines in the file that begin delft.txt
with the letters .J
The output to the standard terminal shows delft.txt
all the lines in the file J
that begin with the letter .
grep
Use command to match end of line
in Linux
grep
The command can also match lines ending with a specific pattern. We use $
regular expressions to match the end of a line.
grep
We have specified the match for the command as
in the following figure rd$
. This means that the grep command should look for lines ending delft.txt
with the string pattern in the file .rd
The two lines that successfully matched the pattern are displayed to the standard terminal.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u