9 正则

 

9.1 正则介绍_grep(上)

正则是什么?
正则就是一串有规律的字符串
掌握好正则对于编写shell脚本有很大帮助
各种编程语言中都有正则,原理是一样的
正则三剑客:
grep/egrep、sed、awk

egep -v "^#^$" /etc/config |wc -l
grep -rn ""
grep    跟下面的参数!
-c    过滤出的关键词 共有多少行数
-i     过滤出的关键词 不区分大小写
-n    过滤出的关键词 显示行号
-v     取反(非关键词行也列出来)
-r    遍历所有子目录(含有关键词行的子目录会紫色标记)
-A     -A跟任意数字(如3):过滤出关键词行和关键词下面任意数字行(如3行)
-B    -B跟任意数字(如3): 过滤出关键词行和关键词上面任意数字行(如3行)
-C    -B跟任意数字(如3):过滤出关键词行和关键词上下各任意数字行(如3行)

[root@hostname grep]#cp /etc/passwd . #拷贝/etc/passwd文件到当前目录下做测试:

 

1. grep -c 过滤出含有关键词行,统计共有多少行 ?  

grep -c '关键词'  文件名

[root@hostname grep]#grep -c 'nologin' passwd

 

-n   显示行号

2. grep -n 过滤出含有关键词行打印到屏幕,并标记行号 

grep -n '关键词'  文件名

[root@hostname grep]#grep -n 'nologin' passwd

 

-i   不区分大小写

3. grep -ni 关键词行打印到屏幕,并标记行号同时关键词不区分大小写

grep -ni '关键词'  文件名   (文件内容中,关键词含有大小写字母都可以筛选出来)

[root@hostname grep]#grep -ni 'nologin' passwd

 

-v   取反

4. grep -vni 不包含关键词行,关键词也不区分大小写,并标记行号打印到屏幕!

grep -vni '关键词'  文件名

[root@hostname grep]#grep -vni 'nologin' passwd

 

-r   遍历所有子目录

5. 筛选出/etc下, 含有关键词行子目录遍历(紫色标记的) :

grep  -r  '关键词'  指定目录

[root@hostname grep]#grep -r 'nologin' /etc/

 

-A   后面跟数字,过滤出符合要求的行以及下面n行

6. grep -nA任意数字

过滤出含有关键词行,同时列出关键词行 下面的2行 

grep -nA2 '关键词'  文件名

[root@hostname grep]#grep -nA2 'rootpasswd

 

-B   同上,过滤出符合要求的行以及上面n行

7. grep -nB任意数字

过滤出含有关键词行,同时列出关键词行 上面的2行 

grep -nB2 '关键词'  文件名

[root@hostname grep]#grep -nB2 'rootpasswd

 

-C   同上,同时过滤出符合要求的行以及上下各n行

8. grep -nC任意数字

过滤出含有关键词行,同时列出关键词行 上面下面2行 

grep -nC2 '关键词'  文件名

[root@hostname grep]#grep -nC2 'rootpasswd

 

9.2 grep(中)  

1. 过滤出 包含0-9数字的行:grep '[数字范围]' 文件名

[root@hostname grep]#grep '[0-9]' passwd

2. 过滤出 不包含0-9数字的行:grep -v '[数字范围]' 文件名

[root@hostname grep]#grep -v '[0-9]' passwd

^放在方括号里面方括号里面字符的反义(非)

^放在方括号外面方括号里面字符开头

3. 过滤出 #开头的行:grep -n '^#文件名

[root@hostname grep]#grep -n '^#' passwd

4. 过滤出 不以#开头的行:grep -nv '^#文件名

[root@hostname grep]#grep -nv '^#passwd

5. 过滤出 含有非0-9数字行(纯数字行不会显示),非数字字符红色标记:

[root@hostname grep]#grep -n '[^0-9]' passwd

6. 过滤出 含有非a-z字母行(纯字母行不会显示),非字母字符红色标记:

[root@hostname grep]#grep -n '[^a-z]' passwd

7. 过滤出 不以0-9数字开头的行:

[root@hostname grep]#grep -n '^[^0-9]' passwd

8. 过滤出 0-9数字开头的行:

[root@hostname grep]#grep -nv '^[^0-9]' passwd

9. 过滤出 不以a-z字母开头的行:

[root@hostname grep]#grep -n '^[^a-z]' passwd

10. 过滤出 a-z字母开头的行:

[root@hostname grep]#grep -nv '^[^a-z]' passwd

9.3 grep(下)

1. 匹配出 含有关键词.关键词的行(表示任意的一个字符):

grep '关键词.关键词文件名

[root@hostname grep]#grep 'r.opasswd

 

2. 匹配出  含有关键词的行(*左边字符可重复任意次):

[root@hostname grep]#grep 'o*o' passwd

 

3. 匹配出 所有字符所有行都匹配了:

[root@hostname grep]#grep '.*' passwd

 

4. 匹配出 包含关键词.*关键词

grep '关键词.*关键词文件名

[root@hostname grep]#grep 'hao.*bash' passwd

 

5. {}里的数字,表示{}前面关键词的重复范围:

[root@hostname grep]#grep 'o\{2\}' passwd

或[root@hostname grep]#egrep 'o\{2\}' passwd

或[root@hostname grep]#grep -E 'o\{2\}' passwd

6. oo组合的2次重复,匹配出不少于4个o的行:

[root@hostname grep]#egrep '(oo){2}' passwd

 

7. +(加号)左边关键词可1次或多次重复,后面匹配关键词t

[root@hostname grep]#egrep 'o+tpasswd

 

8. 前面字符的重复次数为0或1,

[root@hostname grep]#egrep 'o?2o' passwd

 

9. 或者匹配出包含关键词

      (或者)匹配出关键词

      (或者)匹配出包含关键词的行

[root@hostname grep]#grep -E 'root|nologin|haopasswd

 

总结:

.  

任意一个字符

*  

0个或多个*号前面的字符

.*

  通配 所有的都匹配,没有字符也匹配

{}  

花括号里的数字,表示{}前面的字符范围,前面字符可以用()括起来

+

一个或多个加号前面的字符

?  

0个或1个 问号前面的字符

|  

表示 或者,意思

9.4 sed(上)

1. 创建目录 :              

[root@hostname ~]# mkdir sed

2. 进入目录 :                  

[root@hostname ~]# cd sed

3. 拷贝/etc/passwd到当前目录,并重命名 :

[root@hostname sed]# cp /etc/passwd   test.txt

匹配指定行:

1. 匹配 含有关键词(root)

sed -n '/关键词/'p 文件名

[root@hostname sed]# sed -n '/root/'p test.txt

 

2. 匹配 关键词.关键词(点,一个任意字符)的行 :

sed -n '/关键词.关键词/'p 文件名

[root@hostname sed]# sed -n '/r.t/'p test.txt

 

3. 匹配 *星号右边关键词的行,全部打印出来:

sed -n '/任意关键词*关键词/'p 文件名

[root@hostname sed]# sed -n '/r*t/'p test.txt

 

4. 匹配 关键词+关键词(ot组合);sed -nr 作用于 +加号 :

sed -nr '/关键词+关键词/'p 文件名

[root@hostname sed]# sed -nr '/o+t/'p test.txt

 

5. 匹配 关键词任意次(n次) :

sed -nr '/关键词{匹配次数}/'p 文件名

[root@hostname sed]# sed -nr '/o{2}/'p test.txt

 

6. 匹配 关键词1或者匹配关键词2或者匹配关键词3 :

sed -nr '/关键词1|关键词2|关键词3/'p 文件名

[root@hostname sed]# sed -nr '/root|hao|sbin/'p test.txt

 

打印指定行:

7. 打印 指定行

sed -nr '指定行'p 文件名

[root@hostname sed]# sed -nr '2'p test.txt

 

8. 打印 指定范围行

sed -nr '指定范围行'p 文件名

[root@hostname sed]# sed -nr '2,5'p test.txt

 

9. 打印指定行末行($):

sed -nr '指定行,$'p 文件名

[root@hostname sed]# sed -nr '2,$'p test.txt

 

10. 打印全部行(第一行到末行):

sed -nr '1,$'p 文件名

[root@hostname sed]# sed -nr '1,$'p test.txt

11. 打印指定行,还可以匹配包含关键词1的行和匹配包含关键词2的行 :

sed -e '指定行'p -e '/匹配关键词1/'p -e '/匹配关键词2/'p -n 文件名

[root@hostname sed]# sed -e '1'p -e '/111/'p -e '/hao/'p -n test.txt

 

9.5 sed(下)

1. 匹配 含有关键词(root)行,但不区分大小写:

sed -n '/关键词/'Ip 文件名

[root@hostname sed]# sed -n '/root/'Ip test.txt

 

2. 不匹配 指定的范围行,其余行打印到屏幕(只限于生效在屏幕) :

sed '范围行'd test.txt

[root@hostname sed]#sed '1,10'd test.txt

 

3. 删除 文件中指定的范围行(生效在文件):

sed -i '范围行'd test.txt

[root@hostname sed]# sed -i '1,10'd test.txt

查看文件行数:wc -l 文件名

[root@hostname sed]# wc -l test.txt

 

4. 删除 文件中含有关键词的行(生效在文件):

sed -i '/关键词/'d 文件名

[root@hostname sed]# sed -i '/hao/'d test.txt

 

5. 查找指定行,并全局替换 打印到屏幕:

sed '指定范围行s/关键词/替换关键词/g' test.txt

[root@hostname sed]# sed '1,10s/hao/root/g' test.txt

 

head 默认是输出文件内容的前十行!!!

6. 查找指定范围行,并用正则表达式+号进行了全局替换  后面可以用管道符 :

[root@hostname sed]# sed -r '1,10s/ro+/r/g' test.txt |head

7. 把前面命令的输出结果, 管道符丢给sed命令,以冒号分隔为一段,每行的第一段最后一段相互替换

[root@hostname sed]# head test.txt |sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:1/'

 

8. head 输出文件前十行,sed 查找关键词1 替换 关键词2 :

(默认命令用法中分隔符/,可以用@#代替都OK啦)

把关键词:/sbin/nologin  替换成 123 ,因为被替换关键词有/斜杠,就用@为命令分隔符了

[root@hostname sed]# head test.txt |sed 's@/sbin/nologin@123@g'

 

9. 前十行替换, 字母范围为空 

head 文件名 |sed 's/[小写大写字母范围]//g'

[root@hostname sed]# head test.txt |sed 's/[a-zA-Z]//g'

 

10. 所有行行首添加固定的字符串

head 文件名 |sed -r 's/(.*)/添加的字符串:&/'

[root@hostname sed]# head test.txt |sed -r 's/(.*)/hhh:&/'

 

9.6 awk(上)

1. 创建awk目录做测试:

[root@hostname ~]# mkdir awk

2. 进入awk目录下:

[root@hostname ~]# cd awk

3. 拷贝/etc/passwd到当前目录并重命名为test.txt 

[root@hostname ~]# cp /etc/passwd test.txt

1. 打印 所有段($0)所有内容:

awk  '{print 所有段}' 文件名

[root@hostname awk]# awk  '{print $0}' test.txt

2. 打印 以:(冒号)为分隔符, 每行第一段($1):

awk -F '冒号分割符' '{print 第一段}' 文件名

[root@hostname awk]# awk -F ':' '{print $1}' test.txt

3. 打印 以:(冒号)为分隔符, 每行指定段(多个段):

awk -F '冒号分割符' '{print 第一段,第二段,第三段}' 文件名

[root@hostname awk]# awk -F ':' '{print $1,$2,$3}' test.txt

4. 打印 以:(冒号)为分隔符, 每行指定段(多个段),打印内容#号隔开

awk -F ':' '{print 第一段"井号"第二段"井号"第三段}' 文件名

[root@hostname awk]# awk -F ':' '{print $1"#"$2"#"$3}' test.txt

 

5. 打印 匹配包含oo

awk '/匹配关键词/' 文件名

[root@hostname awk]# awk '/oo/' test.txt

 

6. 匹配出 以:(冒号)为分隔符, 第一段中包含oo

awk -F '冒号分隔符' '指定段 ~ /匹配关键词/' 文件名

[root@hostname awk]# awk -F ':' '$1 ~ /oo/' test.txt

 

7. 匹配出 以:(冒号)为分隔符,第一段中包含oo+(支持正则表达式):

awk -F '冒号分隔符' '$1 ~ /匹配关键词+/' 文件名

[root@hostname awk]# awk -F ':' '$1 ~ /oo+/' test.txt

 

8. 匹配出 包含root的行,并打印出此行第一段第四段

                 包含user的行,并打印出此行第二段第三段 

awk -F '冒号分隔符' '/匹配关键词/{print 指定段,指定段} /匹配关键词/ {print 指定段,指定段}' 文件名

[root@hostname awk]# awk -F ':' '/root/{print $1,$4} /user/ {print $2,$3}' test.txt

匹配数字 :数字不加双引号!!!(字符串非数字需要双引号)

>=100     大于等于100

<=100     小于等于100

 =100      等于100

!=100       不等于100

9. 以:(冒号)为分隔符,匹配出第三段大于等于1000的行打印出第一段

( 需求是匹配数字数字不要双引号!)

awk -F '指定分隔符' '指定段>=匹配数字 {print 指定打印段}' 文件名

[root@hostname awk]# awk -F ':'$3>=1000 {print $1}' test.txt

10. :(冒号)为分隔符,匹配出第三段小于等于1000的行打印出全部段

awk -F '指定分隔符' '指定段<=匹配数字 {print 指定打印段}' 文件名

[root@hostname awk]# awk -F ':' '$3<=1000 {print $0}' test.txt

 

11. :(冒号)为分隔符,匹配出第一段不等于关键词hao的行并打印出全部段

( 匹配是非数字字符串,要用双引号引起来! !=  表示不等于)

awk -F '指定分隔符' '指定段!="匹配的字符串"{print 指定打印段}' 文件名

[root@hostname awk]# awk -F ':'$1!="hao"{print $0}' test.txt

9.7 awk(下)

1. 以:(冒号)为分隔符,筛选出第三段数字值小于第四段数字值的行:

(注意  "这里的"数值"不代表数字组合

awk -F '指定分隔符'  '第三段数值<第四段数值'  文件名

[root@hostname awk]# awk -F ':'  '$3<$4'  test.txt

 

2. :(冒号)为分隔符,筛选出第三段数值等于第四段数值的行:

(注意  "这里的"数值"不代表数字组合

awk -F '指定分隔符'  '第三段数值==第四段数值'  文件名

[root@hostname awk]# awk -F ':'  '$3==$4test.txt

 

3. :(冒号)为分隔符,筛选出第三段数值大于5  &&(并且)小于

(注意  "这里的"数值"不代表数字组合

awk -F ':'  '第三段数值>"5" && 第三段数值<"7"文件名

[root@hostname awk]# awk -F ':'  '$3>"5" && $3<"7"test.txt

 

4. :(冒号)为分隔符,筛选出第三段数字大于1000 ||(或者) 第七段数字大于等于/sbin/nologin 

awk -F '指定分隔符' '第三段>指定数字 || 第七段=="指定字符串"' 文件名

[root@hostname awk]# awk -F ':' '$3>1000 || $7=="/sbin/nologin"' test.txt

5. 以:(冒号)为分隔符,筛选出第三段数字大于1000 ||(或者) 第七段匹配关键词nologin的行

awk -F ':' '第三段>指定数字 || 第七段 ~ /匹配关键词/' 文件名

[root@hostname awk]# awk -F ':' '$3>1000 || $7 ~ /nologin/' test.txt

6. :(冒号)为分隔符,筛选出第三段数字大于1000 ||(或者)第七段匹配关键词nologin的行打印出第一段第三段第七段#号隔开!

awk -F '指定分隔符' '{OFS="指定打印分隔符"} 第三段>指定数字 || 第7段 ~ /匹配关键词/ {print 打印第一段,打印第三段,打印第七段}' 文件名

[root@hostname awk]# awk -F ':' '{OFS="#"} $3>1000 || $7 ~ /nologin/ {print $1,$3,$7}' test.txt

7. :(冒号)为分隔符,筛选出第三段数字大于1000的行第一段,第二段,第三段,第四段#号隔开!

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

8. 打印显示行号

[root@hostname awk]# awk -F ':' '{print NR":"$0}' test.txt

9. :(冒号)为分隔符,匹配出每行共有多少行段 

[root@hostname awk]# awk -F ':' '{print NF":"$0}' test.txt

10. 打印出110行:

[root@hostname awk]# awk -F ':' 'NR<10test.txt

11. 以:(冒号)为分隔符,筛选前10&&(并且)第一段含有root或者sync关键词的行:awk -F ':' 'NR<10 && 第一段 ~ /匹配关键词|匹配关键词/' 文件名

[root@hostname awk]# awk -F ':' 'NR<10 && $1 ~ /root|sync/' test.txt

12. :(冒号)为分隔符,匹配前六行 &&(并且)第一段含有root或者sync关键词的行:awk -F ':' 'NF==6 && 第一段 ~ /匹配关键词|匹配关键词/' test.txt

[root@hostname awk]# awk -F ':' 'NF==6 && $1 ~ /root|sync/' test.txt

13. 前三行输出结果,awk执行,以冒号分割符号,复值每行第一段都是root,并把打印出的结果以冒号隔开

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

 

14. 打印行段 不用冒号分割,默认是空格隔开每段:

[root@hostname awk]# head -n 3 test.txt |awk -F ':' '$1="root"'15. :(冒号)为分隔符,设想hao等同于全部行第三段值总和 

[root@hostname awk]# awk -F ':' '{(hao=hao+$3)}; END {print hao}' test.txt

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值