Detailed explanation of grep and egrep commands in Linux

  • 2021-07-03 01:08:38
  • OfStack

rep / egrep

Syntax: grep [-cinvABC] 'word' filename

-c: Number of lines to print
-i: Case ignored
-n: Output with peer number 1 while outputting the line that meets the requirements
-v: Print non-compliant lines
-A: Followed by a number (with or without spaces), for example, A2 means printing the line that meets the requirements and the following two lines
-B: Followed by a number, for example, B2 indicates that the line that meets the requirements and the top two lines are printed
-C: Followed by a number, for example, C2 means printing the line that meets the requirements and two lines at the top and bottom

Print out the line containing 'halt' and the two lines below this line.


[root@localhost ~]# grep -A2 'halt' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

Print out the line containing 'halt' and the two lines above this line.


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

Print out the line containing 'halt' and the two lines above and below this line.

Filter out lines with a certain keyword and output line numbers


[root@localhost ~]# grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

Filter lines without a keyword and output line numbers


[root@localhost ~]# grep -nv 'nologin' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
26:test:x:511:511::/home/test:/bin/bash
27:test1:x:512:511::/home/test1:/bin/bash

Filter out all rows containing numbers


[root@localhost ~]# grep '[0-9]' /etc/inittab
# upstart works, see init(5), init(8), and initctl(8).
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
id:3:initdefault:

Filter out all rows that do not contain numbers


[root@localhost ~]# grep -v '[0-9]' /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
#
# Default runlevel. The runlevels used are:
#

Remove all lines beginning with '#'


[root@localhost ~]# grep -v '^#' /etc/inittab
id:3:initdefault:

Remove all blank lines and lines beginning with '#'


[root@localhost ~]# grep -v '^#' /etc/crontab |grep -v '^$'
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

In regular expressions, "^" represents the beginning of a line, "$" represents the end of a line, and blank lines can be represented by "^ $". How do you print a line that does not start with an English letter?


[root@localhost ~]# vim test.txt
[root@localhost ~]# cat test.txt
123
abc
456
abc2323
#laksdjf
Alllllllll

First, write a few lines of strings in test. txt for experiments.


[root@localhost ~]# grep '^[^a-zA-Z]' test.txt
123
456
#laksdjf

[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
0

If it is a number, use the form [0-9]. Of course, sometimes it can also use the form [15], which only contains 1 or 5. Note that it will not be considered as 15. To filter out numbers and upper and lower case letters, write [0-9a-zA-Z]. There is another form of [], that is, the [^ character] represents characters other than those in [].

Filter any 1 character and duplicate characters


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
1

Represents any 1 character. In the above example, the line with two arbitrary characters between r and o is filtered out, and * represents zero or more previous characters.


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
2

'ooo*' means oo, ooo, oooo... or more 'o' Now do you think of what the combination '. *' means?


[root@localhost ~]# grep '.*' /etc/passwd |wc -l
27
[root@localhost ~]# wc -l /etc/passwd
27 /etc/passwd

'. *' denotes zero or more arbitrary characters, including blank lines.

Specify the number of occurrences of characters to filter


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
4

You can also add-E without the de-signifier\


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
5

Here, {} is used, which is a number inside, indicating the number of times the previous character should be repeated. The above example indicates a line that contains two o, that is, 'oo'. Note that the ambiguous character '\' needs to be added around {}. In addition, we can also indicate a range with {}, and the specific format is' {n1, n2} 'where n1 < n2, which means repeating the preceding character from n1 to n2 times, and n2 can also be empty, which means greater than or equal to n1 times.

grep mentioned in the above part, and egrep is often used. Simply put, the latter is an extended version of the former. We can use egrep to complete the work that grep cannot complete, and of course egrep that grep can complete can be completely completed. If you are too troublesome, egrep can be understood for 1 time, because grep's functions are enough to be competent for your daily work. Here are several uses of egrep that are not used for grep. test. txt is edited as follows for the convenience of the experiment:


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
6

Filter 1 or more preceding characters


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
7

Filter zero or one preceding character


[root@localhost ~]# egrep 'o?' test.txt
rot:x:0:0:/rot:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
1111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# egrep 'ooo?' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'oooo?' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash

Filter string 1 or string 2


[root@localhost ~]# grep -B2 'halt' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
9

Application of () in egrep


[root@localhost ~]# egrep 'r(oo)|(at)o' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash

Use () to denote a whole, for example (oo) + to denote one 'oo' or multiple 'oo'


[root@localhost ~]# egrep '(oo)+' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash

The above is the introduction of Linux grep and egrep command all relevant knowledge points, thank you for learning and support of this site.


Related articles: