Two regular expressions

Creative Commons License Creative Commons

3) Basic metacharacters ^, $ - match the beginning of a line, end of the line
output level of the default run configuration records (beginning with id line):

[root@svr5 ~]# egrep '^id' /etc/inittab
id:3:initdefault:

Output hostname configuration records (beginning with HOSTNAME line):

[root@svr5 ~]# egrep '^HOSTNAME' /etc/sysconfig/network
HOSTNAME=svr5.tarena.com

User statistics the number of local users to log Shell is "/ sbin / nologin" is:

[root@svr5 ~]# egrep -m10 '/sbin/nologin$' /etc/passwd  //先确认匹配正确
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@svr5 ~]# egrep -c '/sbin/nologin$' /etc/passwd
32  									//结合 -c 选项输出匹配的行数

The -c option to match the number of output lines, which again through a pipe wc -l effect is the same, but easier wording. For example, the use of statistics "/ bin / bash" as a normal number of users log on Shell's executable:

[root@svr5 ~]# egrep -c '/bin/bash$' /etc/passwd
26
或者
[root@svr5 ~]# egrep '/bin/bash$' /etc/passwd | wc -l
26

4) Basic metacharacters - matches any single character.
To /etc/rc.local file, for example, to confirm the contents of the text:

[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

Output including at least one character (\ newline except n) of the row /etc/rc.local file, i.e. non-blank line:

[root@svr5 ~]# egrep '.' /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local

Blank line output (-v option with the inverted condition) within the /etc/rc.local file:

[root@svr5 ~]# egrep -v '.' /etc/rc.local

[root@svr5 ~]#

Operation of the emptied row following the same operational effects:

[root@svr5 ~]# egrep '^$' /etc/rc.local

[root@svr5 ~]#

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91947343
Recommended