JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

grep command in Linux

Author:JIYIK Last Updated:2025/03/20 Views:

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.

Previous: None

Next:Adding an empty directory in Git

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.

Article URL:

Related Articles

Check if a file exists using batch processing

Publish Date:2025/03/20 Views:100 Category:OPERATING SYSTEM

This article will demonstrate how to use a batch script to check if a file exists through sample code. Check if a file exists using batch script The general format or syntax of the code to check if a file exists is provided below. IF EXIST

Run the batch file as administrator

Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM

Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch

Running multiple files in a batch script

Publish Date:2025/03/20 Views:55 Category:OPERATING SYSTEM

Large scripts contain multiple files because it is easy to maintain the code. When working with larger scripts, you may want to divide them into modules to make it easier to detect any coding errors or problems. However, Batch does not have

Difference between % and %% in batch files

Publish Date:2025/03/20 Views:200 Category:OPERATING SYSTEM

Batch programmers often confuse single percent signs (%) and double percent signs (%%) when used in batch files. The FOR command uses %f when executed on the command line, but in a batch file, it uses %%f instead of a single percent sign. T

Error handling in batch scripts

Publish Date:2025/03/20 Views:127 Category:OPERATING SYSTEM

Every scripting and programming language contains error handlers, for example Java contains try-catch for error handling. In batch scripts, there is no direct way to do this, but we can create error handlers in batch scripts using the built

Ignore case in Linux grep

Publish Date:2025/03/20 Views:166 Category:OPERATING SYSTEM

In this tutorial, we will learn grep to ignore case in file names using . But first, let's grep start with . Use grep to search each file for a pattern. Patterns lists the patterns separated by newlines, grep outputting each line that match

Adding an empty directory in Git

Publish Date:2025/03/20 Views:167 Category:OPERATING SYSTEM

One of the most important things about git is that it only tracks changes to files, not the folders themselves, which can be found here. Directories are automatically added when you add a file in a directory. Also, when you run git add git

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial