7.3 9.1-9.3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7.3 9.1-9.3相关的知识,希望对你有一定的参考价值。

9.1 正则介绍_grep(上)

 

正则是一串有规律的字符串,通常包含一些特殊符号;

 

grep/egrep egrepgrep的扩展,grep能实现的,egrep均能实现

grep查找文件中的关键字

[[email protected] grep]# grep 'nologin' 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

[[email protected] grep]# which grep

alias grep='grep --color=auto' –color=auto使grep查找到的内容带颜色显示

         /usr/bin/grep

 

-c 查看包含匹配项的行的总数

[[email protected] grep]# grep -c 'nologin' passwd

15 15行包含匹配项

 

-n 显示包含匹配项的行的行号

[[email protected] grep]# grep -n 'nologin' passwd

2:bin:x:1:1:bin:/bin:/sbin/nologin

3:daemon:x:2:2:daemon:/sbin:/sbin/nologin

4:adm:x:3:4:adm:/var/adm:/sbin/nologin

5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

 

-I 匹配时不区分大小写

[[email protected] grep]# grep -in 'nologin' passwd

2:bin:x:1:1:bin:/bin:/sbin/NOLOGIN

3:daemon:x:2:2:daemon:/sbin:/sbin/nologin

4:adm:x:3:4:adm:/var/adm:/sbin/nologin

[[email protected] grep]# grep -n 'nologin' passwd

3:daemon:x:2:2:daemon:/sbin:/sbin/nologin

4:adm:x:3:4:adm:/var/adm:/sbin/nologin

5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

不加-i参数,包含NOLOGIN的第2行不显示

 

-v 将包含关键字nologin的行过滤掉

[[email protected] grep]# grep -nv 'nologin' passwd

1:root:x:0:0:root:/root:/bin/bash

2:bin:x:1:1:bin:/bin:/sbin/NOLOGIN

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

 

-r 遍历指定目录中的所有文件,查找包含关键字的行并打印在屏幕上

[[email protected] grep]# grep -r 'root' /etc/

/etc目录下的所有文件查找包含root关键字的行并打印在屏幕上

/etc/grub.d/00_header:datarootdir="/usr/share" 显示匹配行内容并显示该行所属的文件

/etc/grub.d/00_header:export TEXTDOMAINDIR="${datarootdir}/locale"

/etc/grub.d/00_header:if loadfont `make_system_path_relative_to_its_root "${GRUB_FONT}"` ; then

/etc/grub.d/00_header:    font="`make_system_path_relative_to_its_root "${font_path}"`"

 

-A 过滤出包含关键字的行及该行后的n

[[email protected] grep]# grep -A 2 'root' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

daemon:x:2:2:daemon:/sbin:/sbin/nologin

--

onologinperator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

 

-B 过滤出包含关键字的行及该行以上n

[[email protected] grep]# grep -B 2 'root' passwd

root:x:0:0:root:/root:/bin/bash

--

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

onologinperator:x:11:0:operator:/root:/sbin/nologin

 

-C 过滤出包含关键字的行及该行的上下n

[[email protected] grep]# grep -C 2 'root' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

daemon:x:2:2:daemon:/sbin:/sbin/nologin

--

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

onologinperator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

 

9.2 grep(中)

过滤出带数字的行

[[email protected] grep]# grep '[0-9]' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

daemon:x:2:2:daemon:/sbin:/sbin/nologin

 

过滤出不包含数字的行

[[email protected] grep]# grep -v '[0-9]' passwd

 

过滤出以#开头的行

[[email protected] grep]# grep -n '^#' ./inittab

1:# inittab is no longer used when using systemd.

2:#

3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

4:#

5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target

6:#

7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:

9:# multi-user.target: analogous to runlevel 3 没有第8

 

过滤出不以#开头的行

[[email protected] grep]# grep -nv '^#' ./inittab

8:poiaejroigjpo

 

过滤掉以#开头的行

[[email protected] grep]# grep -v  '^#' ./inittab

poiaejroigjpo

%^*^%%$&*^

 

匹配文件中非数字的项(此时非数字项均被标红)

[[email protected] grep]# grep '[^0-9]' ./inittab

# inittab is no longer used when using systemd.

#

# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

#

# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target

#

# systemd uses 'targets' instead of runlevels. By default, there are two man targets:

poiaejroigjpo

# multi-user.target: analogous to runlevel 3

# graphical.target: analogous to runlevel 5

 

过滤出以非数字开头的行

[[email protected] grep]# grep -n '^[^0-9]' inittab

1:# inittab is no longer used when using systemd.

2:#

3:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

4:#

5:# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target

6:#

7:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:

8:poiaejroigjpo

9:# multi-user.target: analogous to runlevel 3

^[]中表示非(取反),在[]外表示开头

 

9.3 grep(下)

 

. 表示任意一个字符

[[email protected] grep]# grep 'r.o' passwd

root:x:0:0:root:/root:/bin/bash

dfjfjpwejraodroad

onologinperator:x:11:0:operator:/root:/sbin/nologin

[[email protected] grep]# grep 'r.o' passwd

root:x:0:0:root:/root:/bin/bash

dfjfjpwejr.odro.d .也可以匹配到.

onologinperator:x:11:0:operator:/root:/sbin/nologin

 

* 表示将*左侧的字符匹配0n

[[email protected] grep]# grep 'o*o' passwd 匹配前面匹配任意个o,结尾有一个o

root:x:0:0:root:/root:/bin/bash

dfjfjpwejr.odro.d

daemon:x:2:2:daemon:/sbin:/sbin/nologin

rooooooooooot

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

bot 只有单个o

boot o左侧有一个o

boot

booooot

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

 

.* 匹配任意的任意个字符(包括空行)

[[email protected] grep]# grep '.*' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

dfjfjpwejr.odro.d

daemon:x:2:2:daemon:/sbin:/sbin/nologin

rooooooooooot

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

bot

boot

booot

booooot

sync:x:5:0:sync:/sbin:/bin/sync

[[email protected] grep]# grep 'bin.*NOLOGIN' passwd 匹配bin后跟任意个任意字符后接NOLOGIN的项

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

 

o{n} 匹配出现重复n次的字母o

[[email protected] grep]# grep 'o{2}' passwd 匹配字母o出现2次的项(需要脱义)

root:x:0:0:root:/root:/bin/bash

rooooooooooot

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

boot

booot

booooot

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

onologinperator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

 

匹配一个0-n个特定字符

[[email protected] grep]# grep 'o{0,3}' passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/NOLOGIN

dfjfjpwejr.odro.d

daemon:x:2:2:daemon:/sbin:/sbin/nologin

rooooooooooot

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

bot

boot

booot

booooot

[[email protected] grep]# grep -E 'o{2}' passwd grep-E参数不需要脱义符号

root:x:0:0:root:/root:/bin/bash

rooooooooooot

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

boot

booot

booooot

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

onologinperator:x:11:0:operator:/root:/sbin/nologin

[[email protected] grep]# egrep 'o{2}' passwd  使用egrep时不需要脱义符号

root:x:0:0:root:/root:/bin/bash

rooooooooooot

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

boot

booot

booooot

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

onologinperator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

 

[[email protected] grep]# grep -E '(oo){2}' passwd 匹配2次两个o

rooooooooooot

adm:x:3:4:adm:/var/adm:/soooobin/nologin

boooot

sync:x:5:0:sync:/sbin:/bin/syncoooo

 

[[email protected] grep]# grep 'o+o' passwd 匹配+左侧的字符一次或多次

root:x:0:0:root:/root:/bin/bash

rooooooooooot

adm:x:3:4:adm:/var/adm:/soooobin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

boot

booot

boooot

 

[[email protected] grep]# egrep '(oo)?t' passwd 筛选字符串oo后跟一个t的重复次数为01次的

root:x:0:0:root:/root:/bin/bash

rooooooooooot

bot

boot

booot

boooot

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

[[email protected] grep]# egrep 'o?t' passwd 筛选字符o重复次数为01次且后跟一个t

root:x:0:0:root:/root:/bin/bash

rooooooooooot

bot

boot

booot

boooot

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

 

[[email protected] grep]# grep -E 'root|nologin' passwd 匹配rootnologin

root:x:0:0:root:/root:/bin/bash

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/soooobin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin


以上是关于7.3 9.1-9.3的主要内容,如果未能解决你的问题,请参考以下文章

9.1-9.3 grep工具

9.1 第九章 字符集

(07)windows系统安装sonarqube-7.3记录

Python3.7.3安装(Win10)

在 Mac Mojave 上将 PHP 7.2 升级到 7.3

《Go语言实战》摘录:7.3 并发模式 - work