Detailed explanation of grep and egrep commands in Linux

Detailed explanation of grep and egrep commands in Linux

rep / egrep

Syntax: grep [-cinvABC] 'word' filename

-c : Print the number of lines that meet the requirements
-i : Ignore case
-n: Output the line numbers along with the lines that meet the requirements
-v : Print lines that do not meet the requirements
-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, such as B2, which means printing the lines that meet the requirements and the two lines above
-C: followed by a number, such as C2, which means printing the line that meets the requirements and two lines above and below.

Print the line containing 'halt' and the two lines following it.

[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 the line containing 'halt' and the two lines above it.

[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 the line containing 'halt' and the two lines above and below it.

Filter out rows with a certain keyword and output the row number

[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 the lines without a certain keyword and output the line number

[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 starting with '#'

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

Remove all blank lines and lines starting 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, "^" indicates the beginning of a line, "$" indicates the end of a line, and a blank line can be represented by "^$". How do you print lines that do not start with an English letter?

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

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

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

If it is a number, use the format [0-9]. Of course, sometimes you can also use the format [15], which only contains 1 or 5. Note that it will not be considered as 15. If you want to filter out numbers and uppercase and lowercase letters, you should write [0-9a-zA-Z]. In addition, [ ] has another form, which is [^ character], which represents characters other than the characters in [ ].

Filter any character and repeated characters

[root@localhost ~]# grep 'r..o' /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

. represents any character. In the above example, the lines that contain two characters between r and o are filtered out. * represents zero or more of the preceding characters.

[root@localhost ~]# grep 'ooo*' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

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

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

'.*' means zero or more of any characters, including blank lines.

Specify the number of occurrences of the character to be filtered

[root@localhost ~]# grep 'o\{2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

You can also use -E without the escape character \

grep -E 'o{2}' /etc/passwd

{ } is used here, with numbers inside, indicating the number of times the previous character is to be repeated. In the above example, it means the line containing two o's, namely 'oo'. Note that the escape character '\' is required on both sides of { }. In addition, we can also use { } to represent a range. The specific format is '{n1,n2}' where n1<n2 means repeating the previous character n1 to n2 times. n2 can also be empty, which means greater than or equal to n1 times.

The grep mentioned above is also a frequently used tool, egrep. To put it simply, the latter is an extended version of the former. We can use egrep to complete tasks that grep cannot complete. Of course, egrep can also complete tasks that grep can complete. If you find it troublesome, just learn about egrep, because grep's functions are sufficient for your daily work. The following are some usages of egrep that are not used for grep. For the convenience of testing, edit test.txt to the following content:

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
111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Filter one or more preceding characters

[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
[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
[root@localhost ~]# egrep 'ooo+' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash

Filters for zero or one occurrence of the 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
111111111111111111111111111111
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 ~]# egrep 'aaa|111|ooo' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

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 represent a whole, for example (oo)+ means 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 all the relevant knowledge points of Linux grep and egrep commands introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of using grep command in Linux
  • Grep command to match tabs and newlines in Linux
  • Linux uses grep to filter multiple conditions and grep common filtering commands
  • How to retrieve file contents using grep command in Linux
  • Common commands and differences between find and grep in Linux
  • Introduction to Linux text filtering grep basic commands (5)
  • Detailed explanation of Linux grep command
  • In-depth understanding of grep command in Linux
  • Grep command usage in Linux
  • Detailed explanation of the usage of grep command in Linux

<<:  Description of the writing method of foreach array in Vue and traversal array in js

>>:  Detailed explanation of the error problem when setting the default value of 0000-00-00 in the date and datetime types of Mysql database

Recommend

Docker deployment of Flask application implementation steps

1. Purpose Write a Flask application locally, pac...

Analysis of centos6 method of deploying kafka project using docker

This article describes how to use docker to deplo...

How to bind domain name to nginx service

Configure multiple servers in nginx.conf: When pr...

Display and hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

js to realize web message board function

This article example shares the specific code of ...

Summary of commonly used operators and functions in MySQL

Let’s build the data table first. use test; creat...

React encapsulates the global bullet box method

This article example shares the specific code of ...

How to monitor global variables in WeChat applet

I recently encountered a problem at work. There i...

About nginx to implement jira reverse proxy

Summary: Configure nginx reverse proxy jira and i...

TypeScript interface definition case tutorial

The role of the interface: Interface, in English:...

Vue+openlayer5 method to get the coordinates of the current mouse slide

Preface: How to get the coordinates of the curren...

Mac VMware Fusion CentOS7 configuration static IP tutorial diagram

Table of contents Install CentOS7 Configuring Sta...

How to implement mysql database backup in golang

background Navicat is the best MySQL visualizatio...

Detailed explanation of mysql basic operation statement commands

1. Connect to MySQL Format: mysql -h host address...