grep command in Linux
This tutorial demonstrates the use of grepthe 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.
grepCommands
in Linux
grepCommand stands for Global Regular Expression Print. grepCommand searches for a particular pattern in a given file.
Once a match is found, grepthe command displays all lines that match the pattern. grepThe pattern the command searches for is a regular expression.
grepThe command uses the following syntax.
grep [options] pattern [file]
grepUse command to match case insensitive pattern
in Linux
grepcommand can match the pattern without case sensitivity. We use the command with -r -ioption grepto match the pattern ignoring case sensitivity.
-iThe -p option tells grepthe command to ignore case differences between the pattern and the data in the file.
The following image demonstrates the use of grepcommand to match a pattern ignoring the case difference between pattern and data. We are currently in foothe directory, which has delft.txtfile .
Use catthe command to delft.txtprint the contents of the file to standard output. We use the command with -ithe option grepto match delft.txtthe pattern in the file Havard.
The output shows that two matches of delft.txtare found in . There are different cases for the matching pair patterns in the file.Havarddelft.txt
This is because the -p option grepin the command -itells grepthe command to ignore case differences between the pattern and the data in the file.
grepCount the number of matches in Linux
using command
grepThe command can also count the number of correct matches. The command with -cthe -p option grepprints the count of correct matches.
In the following image, we use the -p command with -ithe -d option grepto check delft.txtthe pattern in Havard. The command finds two correct matches.
We then use the same command, but this time, we include -cthe -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.txtthe pattern in the file .Havard
Use command in Linux grepto provide matching file name
grepThe command can give the file names that contain a match. Using the command with -lthe option grepwill display the file names that contain a match with the pattern.
We illustrate how to use grepthe command to return the names of files that match a pattern. We fooare working in the directory, which has two files, delft.txtand example.txt.
Use catthe command to display the contents of the two files to standard output.
We use the command with -lthe -p option grepand set the pattern to text, passing in a wildcard *as grepthe last argument to the command.
-lThe -p option tells grepthe command to display only the names of the files that contain a match, rather than the lines within the files.
The wildcard character *tells grepthe command to search all files in the current directory.
grepThe command prints the file names example.txtas a pattern match is found in this file.
grepUse command to match exact pattern
in Linux
We can also use grepthe -p command to match an exact pattern. To do this, we use the -p command with -wthe -p option grep.
The following image shows the use of grepthe command to match an exact pattern. We have a delft.txttext file called . We use catthe command to delft.txtdisplay the contents of the file to standard output.
Use the command with -wthe -p option grepto match an exact pattern delft.txtin a file Havard. -wThe -p option tells grepthe command to match the entire pattern.
This option respects case differences between the pattern and the data in the file.
grepThe command displays lines with the exact pattern to standard output.
Use command in Linux grepto display only matching patterns
Using grepthe -p command, we can display only the portion of the line that contains a match. We use the -p command with -othe -p option grep.
We are fooin the directory, which contains the file in the image below delft.txt. We use catthe command to display the contents of the file to standard output.
Use the -o-p command with the -p option grepto match delft.txtpatterns in a file Yale. Pattern matching takes into account case differences between the pattern and the data in the file. -oThe -p option tells grepthe -p command to print only the matching portion of the line.
The output display grepcommand displays only the portion that matches the pattern.
grepUse command to display line number
in Linux
Using grepthe -p command, we can also display the line numbers that contain pattern matches. We can use the -p command with the -p -noption grepto display the line numbers that contain pattern matches.
In the image below, we fooare working in the directory.
fooThe directory contains a delft.txtfile called . We use catthe command to delft.txtprint the contents of to standard output.
Use the -p command with the -p -iand -n-p options grepto match delft.txtpatterns in a file Yale. The -i-p grepcommand ignores the case differences between the pattern and the data in the file. -nThe -p option tells grepthe -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.
grepInvert Pattern Matching
in Linux Using Command
Using grepthe -p command, we can display all the files that do not contain lines matching a pattern. We can use the -p command with -vthe -p option grep.
-vThe -p option tells grepthe command to display only those lines that do not match the supplied pattern.
In the image below, we fooare working in the directory.
fooThe directory has delft.txtthe file. We use catthe command to delft.txtdisplay the contents of the file to standard output.
We use the command with -vthe -p option grepto display all the lines that do not match the -p pattern delft.txtin the -p file . The output displays all the lines that do not contain the -p pattern.MITMIT
grepUse command to match the starting string
in Linux
We can use grepthe 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 grepspecified the match for the command as ^J. This means that grepthe command should look for lines in the file that begin delft.txtwith the letters .J
The output to the standard terminal shows delft.txtall the lines in the file Jthat begin with the letter .
grepUse command to match end of line
in Linux
grepThe command can also match lines ending with a specific pattern. We use $regular expressions to match the end of a line.
grepWe 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.txtwith 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

