第九章正则表达式预习笔记加课堂笔记

9.1 正则介绍_grep(上)

grep 过滤关键字会有颜色显示

grep  -c  'nologin' passwd            -c 统计行数   count

grep  -n 'nologin' passwd           -n 显示行号  number

grep -i 'nologin' passwd            -i  不区分大小写 

grep -vni 'nologin' passwd             -v   取反

grep -r 'root' /etc/                 -r   遍历所有子目录

如果不加 -r 仅仅是一个目录

grep -nA2 'root' passwd     -A  过滤出符合要求的行,后面跟数字,可以过滤出下面的n行 

grep -nA2 'root' passwd      -B 过滤出符合要求的行,后面跟数字,可以过滤出上面的n行

grep -nC2 'root' passwd    -C 过滤出符合要求的行,后面跟数字,可以过滤出上下的n行

9.2 grep(中)

grep的具体用法

grep [0-9] inittab  过滤出包含数字的行

grep -v [0-9] inittab  把不包含数字的行过滤出来

grep -nv "^#" inittab  把不以#号开头的行过滤出来

grep '[^0-9]' inittab  把非0到9的内容过滤出来

grep '^[^0-9]' inittab             把不以数字开头的行过滤出来

grep -vn '^[^0-9]' inittab       加v取反,把数字开头的行过滤出来

9.3 grep(下)

grep 'r.o' passwd     .  点就是匹配任意一个字符

passwd里面添加了一些内容

grep 'o*o' passwd    *表示零个或多个*号前面的字符

grep '.*' passwd   .* 代表贪婪匹配

grep 'aming.*bash' passwd

grep 'o\{2\}' passwd         { } 其内部的数字,表示前面的字符重复次数

{ } 还可以表示一个范围,格式为{n1,n2} 其中n1<n2,n2可以为空

如果不想使用脱义符,可以使用egrep工具,grep 加大写的E选项也可以实现

egrep 'o{2}' passwd

grep -E '0{2}' passwd

grep -E '(oo){2}' passwd  也可以使用小括号组合起来,代表一个组合,

例子代表一个或多个oo

egrep 'o+o' passwd   +  代表匹配一个和多个加号前面的字符,需要用egrep,不然需要加脱义符

egrep 'o+t' passwd  加号后面也可以跟其他字符,需要用egrep,不然需要加脱义符

egrep 'o?1o' passwd    ? 代表匹配零个或多个?号前面的字符

egrep 'root|nologin' passwd      | 竖线代表或者,匹配root或nologin

 -i 不区分大小写

9.4 sed(上)

sed 工具可以查找和替换的功能,比grep工具功能强大

sed命令的格式   sed -n 'n'p filename

以下功能是实现像grep一样的查找功能,^ . *  +  {}  |  $ 这些特殊符号的使用和grep使用方法是一样的

sed -n '/root/'p test.txt   

sed -n '/^1/'p test.txt

sed -n '/in$/'p test.txt

sed -n '/r.t/'p test.txt

sed -n '/r*t/'p test.txt

sed -nr '/o+t/'p test.txt

sed -nr '/o{2}/'p test.txt

sed -nr '/root|bus/'p test.txt

sed -n '2'p test.txt  打印第二行(指定行)

sed -n '2,5'p test.txt  打印2到5行  (指定一个范围)

sed -n '25,$'p test.txt  打印25行到结尾  

sed -n '1,$'p test.txt  打印全文

sed -e '1'p -e '/bus/'p -n test.txt   -e 选项可以实现多个行为,也可以说可以指定多个查找条件

sed -i '1'd test.txt 

删除指定的行号加d 

sed  '1,25'd test.txt  删除1到25行 ,指定一个范围,这样没有实际删除,需要加上-i 选项 

sed -i '1,25'd test.txt 加上-i 选项就是实际删除了

sed -i '/user1/'d test.txt  还可以删除匹配的字符

sed '1,10s/root/toor/g' test.txt  s就是替换的意思,g为全局替换,否则只替换第一次

sed -r '1,10s/ro+/r/g' test.txt | head       匹配ro+要加选项 -r ,sed不识别+ | { } 等符号,需要借助脱义符号

head test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'   sed 可以用()表示一个整体,本例把行首和行尾调换位置,后面的1\2\3分别表示第一个,第二个,第三个小括号里面的内容。

head test.txt | sed 's/\/root/123/g'    把/root替换成123  /root前面要加个反斜杠 \  

head test.txt | sed 's@/sbin/nologin@123@g'  如果不使用上面的反斜杠可以使用 @,# 符号

head test.txt | sed 's/[a-zA-Z]//g'  把所有字母替换为空,相当于删除所有大小写字母,

head test.txt |sed -r 's/(.*)/aaa:&/'   & 符号的作用是给行首添加字符串,&前面是添加的内容

9.6 awk(上)

awk -F ':' '{print $1}' test.txt

awk -F ':' '{print $0}' test.txt

awk '{print $0}' test.txt

如果不指定分隔符,默认使用空格,空白字符为分隔符

awk '{print $1}' 1.txt

awk -F ':' '{print $1,$2,$3}' test.txt 

awk '/oo/' test.txt

awk -F ':' '$1 ~ /oo/' test.txt

awk -F ':' '$1 ~ /oo+/' test.txt

awk -F ':' '$1 ~ /o+/' test.txt

awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt

awk -F ':' '/root|user/ {print $0}' test.txt

awk -F ':' '$3==0 {print $1}' test.txt

awk -F ':' '$3>=1000 {print $1}' test.txt

awk -F ':' '$3>=1000 {print $0}' test.txt

awk -F ':' '$7!="/bin/nologin" {print $0}' test.txt

9.7 awk(下)

awk -F ':' '$3<$4' test.txt

awk -F ':' '$3==$4' test.txt

awk -F ':' '$3>"5" && $3<"7"' test.txt

awk -F ':' '$3>1000 || $7=="/sbin/nologin"' test.txt

awk -F ':' '$3>1000 || $7 ~ /bash/' test.txt

awk -F ':' '{OFS="#"} $3>1000 || $7 ~ /bash/  {print $1,$3,$4}' test.txt

awk -F ':' '{OFS="#"} {print $1,$3,$4}' test.txt

awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' test.txt

awk -F ':' '{OFS="#"} ($3>1000) {print $1,$2,$3,$4}' test.txt

awk -F ':' '{print NR":"$0}' test.txt

awk -F ':' '{print NF":"$0}' test.txt

awk -F ':' '{print NF":"$0}' test.txt

awk -F ':' 'NR<=10' test.txt

awk -F ':' 'NR<=10 && $1 ~ /root|sync/' test.txt

awk -F ':' 'NF==6 && $1 ~ /root|sync/' test.txt

head -n 3 /etc/passwd | awk -F ':' '$1="root"'

head -n 3 test.txt | awk -F ':' '{OFS=":"} $1="root"'

awk -F ':' '{(tot=tot+$3)}; END {print tot}' test.txt

 

第九章正则表达式课堂笔记

  1. 正则
    1. 什么是正则
    2. grep
    3. gerp/egrep正则示例
    4. sed
    5. awk
  2. 拓展

正则

什么是正则

egrep是grep的扩展,grep有的功能egrep都有

grep

过滤指定关键词 

语法

grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

参数

-a或--text 不要忽略二进制的数据。 -A<显示列数>或--after-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之后的内容。 -b或--byte-offset 在显示符合范本样式的那一列之前,标示出该列第一个字符的位编号。 -B<显示列数>或--before-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前的内容。 -c或--count 计算符合范本样式的列数。 -C<显示列数>或--context=<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。 -d<进行动作>或--directories=<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。 -e<范本样式>或--regexp=<范本样式> 指定字符串做为查找文件内容的范本样式。 -E或--extended-regexp 将范本样式为延伸的普通表示法来使用。 -f<范本文件>或--file=<范本文件> 指定范本文件,其内容含有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每列一个范本样式。 -F或--fixed-regexp 将范本样式视为固定字符串的列表。 -G或--basic-regexp 将范本样式视为普通的表示法来使用。 -h或--no-filename 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。 -H或--with-filename 在显示符合范本样式的那一列之前,表示该列所属的文件名称。 -i或--ignore-case 忽略字符大小写的差别。 -l或--file-with-matches 列出文件内容符合指定的范本样式的文件名称。 -L或--files-without-match 列出文件内容不符合指定的范本样式的文件名称。 -n或--line-number 在显示符合范本样式的那一列之前,标示出该列的列数编号。 -q或--quiet或--silent 不显示任何信息。 -r或--recursive 此参数的效果和指定"-d recurse"参数相同。 -s或--no-messages 不显示错误信息。 -v或--revert-match 反转查找。 -V或--version 显示版本信息。 -w或--word-regexp 只显示全字符合的列。 -x或--line-regexp 只显示全列符合的列。 -y 此参数的效果和指定"-i"参数相同。 --help 在线帮助。

  1. 简单过滤 grep ‘过滤词’ 文件名

[root@linux7-128 grep]# grep 'nologin' passwd

在centos7里grep自动带有颜色区分,6是没有颜色的

[root@linux7-128 grep]# grep -c 'nologin' passwd

38

2、在前面显示行号 -n

[root@linux7-128 grep]# grep -n 'nologin' passwd

3、取反 -v

把不含有nologin的行检索出来,并且显示行号

4、遍历所有子目录 -r

可以把/etc/目录下所有含有root的文件都找到

5、过滤出符合要求的行以及下面n行 -An

[root@linux7-128 grep]# grep -nA2 'root' passwd

6、过滤出符合要求的行以及上面n行 -Bn

# grep -nB2 'root' passwd

7、过滤出符合要求的行以及上下n行 -Cn

[root@linux7-128 grep]# grep -nC2 'root' passwd

gerp/egrep正则示例

[ ]表示含有至少其中一个字符

  1. grep ‘[0-9]’ 文件名

检索出文件里含有数字0-9的,加-n显示行号,便于区分

[root@linux7-128 grep]# grep -n '[0-9]' passwd

也可以检索出文件里不包含0-9的内容 ,加上-v

[root@linux7-128 grep]# grep -nv '[0-9]' passwd

[root@linux7-128 grep]# grep -nv '[0-9]' /etc/inittab

  1. grep ‘^#’ 文件名

检索出以#号开头的内容

[root@linux7-128 grep]# grep '^#' /etc/sos.conf

也可以使用-v检索出不以#号开头的内容

[root@linux7-128 grep]# grep -v '^#' /etc/sos.conf

  1. grep ‘[^0-9]’ 文件名

检索出不含有数字0-9的内容,符合要求的会变红

加上-v会显示只含有数字0-9的内容

[root@linux7-128 grep]# grep -v '[^0-9]' inittab

  1. grep ‘^[^0-9]’ 文件名

检索出不以数字开头的内容

[root@linux7-128 grep]# grep '^[^0-9]' inittab

加上-v会显示以数字开头的内容

[root@linux7-128 grep]# grep -v '^[^0-9]' inittab

总结: 

^ 

放到外面表示以什么什么开头,如果放到[]里,意味着是[]里的字符的反义、非意

. 表示任意的一个字符

  1. grep ‘字符.字符’ 文件

以左边字符开头,右边字符结尾的内容

[root@linux7-128 grep]# grep 'r.o' passwd

* 表示*号左边的字符重复0-n次

  1. grep ‘字符*字符’ 文件

[root@linux7-128 grep]# grep 'h*w' passwd

  1. grep ‘.*’ 文件

把所有文件内容检索出来

[root@linux7-128 grep]# grep 'hu.*bash' passwd

{} 表示前面字符的重复范围

  1. grep ‘o{2}’ passwd 

检索出o出现2次的内容

[root@linux7-128 grep]# grep 'o\{2\}' passwd

egrep ‘o{1,3}’ passwd 

检索出o出现1-3次的内容

egrep ‘o{2}’ passwd= grep -E ‘o{2}’ passwd=grep ‘o\{2\}’ passwd 

*检索出o出现2次的内容,使用egrep可以不使用脱义符*

[root@linux7-128 grep]# egrep 'o{2}' passwd

  1. egrep ‘(oo){2}’ passwd 

检索出oo出现2次的内容

[root@linux7-128 grep]# egrep '(oo){2}' passwd huhu5:x:1010:1009::/hoooome/huhu5:/bin/bash

+ 表示前面字符的1次或n次 

*相比是他从1次开始,*是从0次开始

  1. grep ‘o\+o’ passwd =egrep ‘o+o’ passwd

[root@linux7-128 grep]# grep 'o\+o' passwd

‘?’表示?前面的字符重复次数为0或1次

[root@linux7-128 grep]# egrep 's?dw' passwd wdwwwww zxxsdww

| 表示或者,可以写多个

[root@linux7-128 grep]# egrep 'root|huhu' passwd

()表示一个组合

  1. egrep ‘(oo){2}’ passwd 

检索出oo出现2次的内容

[root@linux7-128 grep]# egrep '(oo){2}' passwd

sed

sed和grep相比,sed也能实现grep的功能,但是有点麻烦,而且没有用颜色显示,强项在于替换。

匹配指定字符串 

sed -n ‘/字符串/’p 文件

[root@linux7-128 sed]# sed -n '/root/'p passwd

使用*?.+{}|^、……

[root@linux7-128 sed]# sed -n '/r*t/'p passwd

[root@linux7-128 sed]# sed -n '/r.t/'p passwd

[root@linux7-128 sed]# sed -n '/^t/'p passwd

+、?号需要使用脱义符

[root@linux7-128 sed]# sed -n '/r\+t/'p passwd

[root@linux7-128 sed]# sed -n '/r\?t/'p passwd

或者使用sed -nr

[root@linux7-128 sed]# sed -nr '/r+t/'p passwd

[root@linux7-128 sed]# sed -nr '/r?t/'p passwd

[root@linux7-128 sed]# sed -nr '/o{2}/'p passwd

[root@linux7-128 sed]# sed -nr '/root|huhu/'p passwd

打印指定行

打印第二行 [root@linux7-128 sed]# sed -n '2'p passwd

打印第45到最后一行 [root@linux7-128 sed]# sed -n '45,$'p passwd

先匹配出第一行,在匹配出含有root字符串的行

[root@linux7-128 sed]# sed -e '1'p -e '/root/'p -n passwd

不区分大小写匹配 I

[root@linux7-128 sed]# sed -n '/root/'Ip passwd

删除匹配行 sed -i

只是在屏幕上删除,显示剩余的行内容

[root@linux7-128 sed]# sed '1,45'd

在文件里根据行数删除

[root@linux7-128 sed]# wc -l passwd50 passwd

[root@linux7-128 sed]# sed -i '1,45'd passwd

[root@linux7-128 sed]# wc -l passwd5 passwd

根据指定字符串删除 [root@linux7-128 sed]# sed -i '/yang/'d passwd

  1. sed ‘1,10s/root/toor/g’ passwd 

1-10行的root替换为toor

会把所有的内容打印出来

  1. sed -r ‘1,10s/ro+/r/g’ passwd |head 

把1-10行的ro+替换为r,即之前的root现在变为了rt

[root@linux7-128 sed]# sed -r '1,10s/ro+/r/g' passwd

  1. sed -r ‘s/([^:]+):(.*):([^:]+)/\3:\2:\1/’ 

把第一段和最后一段调换位置

[root@li

[^:]+ : 非冒号,一个或多个 1

:(.*): :贪婪匹配,一直匹配到最后一个冒号 2

[^:]+ :非冒号,一个或多个 3

前面用小括号()括起来的,后面用反斜杠\数字去表示

  1. 替换内容时,有/,需要用\

[root@linux7-128 sed]# head passwd |sed 's/\/sbin\/nologin/123/g'

# head passwd |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/'

或者使用@、#等替换分隔符

[root@linux7-128 sed]# head passwd |sed 's@/sbin/nologin@123@g'

  1. 把英文字母全部删除,相当于替换为空 

sed 's/[a-zA-Z]//g

[root@linux7-128 sed]# head passwd |sed 's/[a-zA-Z]//g'

  1. 把所有行前面加一个字符串 

sed -r 's/(.*)/aaa:&/'

&表示前面的小括号()

[root@linux7-128 sed]# head passwd |sed -r 's/(.*)/aaa:&/'

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值