Sort out some common grep commands in Linux

Time:2022-8-3

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

   

Copy code

The code is as follows:

[email protected]:~# grep linuxtechi /etc/passwd
linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#

Example 2 find patterns in multiple files.

   

Copy code

The code is as follows:

[email protected]:~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow
/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.

   

Copy code

The code is as follows:

[email protected]:~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab
/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

   

Copy code

The code is as follows:

[email protected]:~# grep -n linuxtechi /etc/passwd
39:linuxtechi:x:1000:1000:linuxtechi,,,:/home/linuxtechi:/bin/bash
[email protected]:~#</p>
<p> [email protected]:~# grep -n root /etc/passwd /etc/shadow

    

2015529184845195.jpg (552×81)   

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”

   

Copy code

The code is as follows:

[email protected]:~# grep -v linuxtechi /etc/passwd

   

  2015529184927778.jpg (676×279)

  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

   

Copy code

The code is as follows:

[email protected]:~# grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[email protected]:~#

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.

   

Copy code

The code is as follows:

[email protected]:~# grep bash$ /etc/passwd
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

  

Copy code

The code is as follows:

[email protected]:~# grep -r linuxtechi /etc/
/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

   

Copy code

The code is as follows:

[email protected]:~# grep ^$ /etc/shadow
[email protected]:~#

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.

   

Copy code

The code is as follows:

[email protected]:~# grep -e “linuxtechi” -e “root” /etc/passwd
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.

   

Copy code

The code is as follows:

[email protected]:~# cat grep_pattern
^linuxtechi
root
false$
[email protected]:~#

Now, try grep_ Pattern file to search

   

Copy code

The code is as follows:

[email protected]:~# grep -f grep_pattern /etc/passwd

   

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

   

Copy code

The code is as follows:

[email protected]:~# grep -c -f grep_pattern /etc/passwd
22
[email protected]:~#

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

  

Copy code

The code is as follows:

[email protected]:~# grep -B 4 “games” /etc/passwd

   

2015529185157783.jpg (527×156)

b) Use the -a parameter to output the last 4 lines of the matching line

  

Copy code

The code is as follows:

[email protected]:~# grep -A 4 “games” /etc/passwd

2015529185244342.jpg (573×140)

c) Use the -c parameter to output 4 lines before and after the matching line

  

Copy code

The code is as follows:

[email protected]:~# grep -C 4 “games” /etc/passwd

2015529185422170.jpg (541×202)

Recommended Today

Method of synthesizing red hat 9.0 CD ISO into a DVD ISO

Combine three red hat 9.0 CD ISOs into one DVD ISOmethod1. First of all, you must have a large disk space of about 3.5G. If you are free, create a document folder createiso, and then put your three ISO documents shreke-i386-disk1.iso, shreke-i386-disk2.iso, shreke-i386-disk3.iso into createiso2. Enter the following command   mkdir shrike-i386-disc{1,2,3} shrike-docs This is to […]