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
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
Waiting for commands in a Windows batch file to finish executing
Publish Date:2025/03/20 Views:98 Category:OPERATING SYSTEM
-
There are multiple commands and installation processes in a batch file, which usually takes some time to complete. However, when a batch file is run, it does not wait for the command process to complete; it executes all the commands line by
Redirecting output to a text file from within a batch file
Publish Date:2025/03/20 Views:186 Category:OPERATING SYSTEM
-
This article will explain different ways to redirect output from a text file. Redirection operators in batch scripts We can redirect the output of a batch file to a text file using redirection operators. Redirection operators redirect the i
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