summary:
All Linux like systems will provide a search tool called grep (Global regular expression print). The grep command is very useful in the case of pattern based search of the contents of one or more files. The pattern can be a single character, multiple characters, a single word, or a sentence.
When the command matches the pattern specified when executing the command, grep will output a line containing the pattern, but will not modify the contents of the original file.
In this article, we will discuss 14 examples of grep commands.
Example 1 find patterns (words) in files
Look for the word “linuxtechi” in the /etc/passwd file
The code is as follows:
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#
Example 2 find patterns in multiple files.
The code is as follows:
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/shadow:linuxtechi:$6$DdgXjxlM$4flz4JRvefvKp0DG6re:16550:0:99999:7:::/etc/gshadow:adm:*::syslog,linuxtechi
/etc/gshadow:cdrom:*::linuxtechi
/etc/gshadow:sudo:*::linuxtechi
/etc/gshadow:dip:*::linuxtechi
/etc/gshadow:plugdev:*::linuxtechi
/etc/gshadow:lpadmin:!::linuxtechi
/etc/gshadow:linuxtechi:!::
/etc/gshadow:sambashare:!::linuxtechi
[email protected]:~#
Example 3 uses the -l parameter to list the file name of the file containing the specified schema.
The code is as follows:
/etc/passwd
/etc/shadow
[email protected]:~#
Example 4 use the -n parameter to find the specified pattern in the file and display the line number of the matching line
The code is as follows:
39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#</p>
<p> [email protected]:~# grep -n root /etc/passwd /etc/shadow
Example 5 use the -v parameter to output lines that do not contain the specified mode
Output all lines in the /etc/passwd file without the word “linuxtechi”
The code is as follows:
Example 6 use the ^ symbol to output all lines beginning with a specified mode
Bash scripts treat the ^ symbol as a special character, which is used to specify the beginning of a line or a word. For example, output all lines beginning with “root” in the /etc/passes file
The code is as follows:
Example 7 uses the $symbol to output all lines ending in the specified mode.
Output all lines ending in “bash” in the /etc/passwd file.
The code is as follows:
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#
Bash scripts treat the dollar ($) symbol as a special character that specifies the end of a line or word.
Example 8 recursively finds a specific pattern using the -r parameter
The code is as follows:
/etc/subuid:linuxtechi:100000:65536
/etc/group:adm:x:4:syslog,linuxtechi
/etc/group:cdrom:x:24:linuxtechi
/etc/group:sudo:x:27:linuxtechi
/etc/group:dip:x:30:linuxtechi
/etc/group:plugdev:x:46:linuxtechi
/etc/group:lpadmin:x:115:linuxtechi
/etc/group:linuxtechi:x:1000:
/etc/group:sambashare:x:131:linuxtechi
/etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
/etc/passwd:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
………………………………………………………………….
The above command will recursively find the word “linuxtechi” in the /etc directory
Example 9 use grep to find all empty lines in the file
The code is as follows:
Since there is no blank line in the /etc/shadow file, there is no output
Example 10 use the -i parameter to find the mode
The -i parameter of the grep command ignores the case of characters when searching.
Let’s take an example to find the word “linuxtechi” in the paswd file.
[code ][email protected]:~$ grep -i LinuxTechi /etc/passwd
linuxtechi:x:1001:1001::/home/linuxtechi:/bin/bash
[email protected]:~$ [/code]
Example 11 use the -e parameter to find multiple patterns
For example, I want to find the words’ linuxtechi ‘and’ root ‘in a grep command. Using the -e parameter, we can find multiple patterns.
The code is as follows:
root:x:0:0:root:/root:/bin/bash
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#
Example 12 use -f to specify the mode to be found with a file
First, create a search pattern file “grep_pattern” in the current directory. I want to enter the following contents in the file.
The code is as follows:
Now, try grep_ Pattern file to search
The code is as follows:
Example 13 use the -c parameter to calculate the number of pattern matches
Continuing with the above example, we use the -c command in the grep command to calculate the number of matching specified patterns
The code is as follows:
Example 14 output matches the N lines before or after the specified pattern line
a) Use the -b parameter to output the first 4 lines of the matching line
The code is as follows:
b) Use the -a parameter to output the last 4 lines of the matching line
The code is as follows:
c) Use the -c parameter to output 4 lines before and after the matching line
The code is as follows: