shell编程-正则表大式练习题讲解、sed

shell编程-正则表大式练习题讲解、sed
正则习题讲解:

 sed    awk 

正则表达式练习作业:

1.[root@cml ~]# egrep ‘the’ regularexpress.txt
2.[root@cml ~]# egrep -v ‘the’ regularexpress.txt
3.[root@cml ~]# egrep -i ‘the’ regularexpress.txt
4.[root@cml ~]# egrep ‘test|taste’ regularexpress.txt
5.[root@cml ~]# egrep ‘oo’ regularexpress.txt
6.[root@cml ~]# egrep -V ‘[^g]oo’ regularexpress.txt
7.[root@cml ~]# egrep ‘[^a-Z ]oo’ regularexpress.txt
8.[root@cml ~]# egrep -V ‘[0-9]’ regularexpress.txt
9.[root@cml ~]# egrep ‘^the’ regularexpress.txt
10.[root@cml ~]# egrep ‘1’ regularexpress.txt
11.[root@cml ~]# egrep -v ‘2’ regularexpress.txt
12.[root@cml ~]# egrep ‘.KaTeX parse error: Expected 'EOF', got '#' at position 37: …13.[root@cml ~]#̲ egrep '^’ regularexpress.txt
14.[root@cml ~]# egrep ‘g…d’ regularexpress.txt
15.[root@cml ~]# egrep ‘oo+’ regularexpress.txt
16.[root@cml ~]# grep ‘go{2,}g’ regularexpress.txt
17.[root@cml ~]# egrep -v ‘[0-9]’ regularexpress.txt
18.[root@cml ~]# grep ‘o{2}’ regularexpress.txt
19.[root@cml ~]# grep ‘go{2,5}g’ regularexpress.txt
20.[root@cml ~]# grep ‘go{2,}’ regularexpress.txt

1.# grep -n ‘the’ test. txt

2.# grep -vn ‘the’ test. txt

3.# grep -in ‘the’ test. txt

4.# grep -n ‘test|taste’ test. txt
test I tast --> t[ae]st

5.# grep -n ‘oo’ test. txt

6.# grep -n ‘[^g]oo’ test. txt

7.# grep -n ‘[^a-z]oo’ test. txt

grep -n [3]oo’ test. txt (小写)

8.# grep -n ‘0-9’ test. txt

grep -n ‘[[:digit:]]’ test. txt (数字)

9.# grep -n '^the’test. txt

10.# grep -n ‘[a-z]’ test. txt

grep -n ‘4’ test. txt

11.# grep -n ‘[a-zA-Z]’ test. txt

12.# grep -n ‘.$’ test. txt

13.# grep -n ‘^$’ test . txt

14.# grep -n ‘g…d’ test . txt

15.# grep -n ‘ooo*’ test . txt

16.# grep -n ‘goo*g’ test . txt

17.# grep -n ‘[0-9][0-9]*’ test . txt

18.# grep -n ‘o{2}’ test . txt

19.# grep -n ‘go{2,5}g’ test . txt

20.# grep -n ‘o{2,}’ test . txt

键部署LNMP:
键部署DHCP、DNS,FTP、LAMP、Mysql、 samba、nfs ,NTP

sed 直接处理文本
删除/etc/ passwd文件中的匹配的行
awk 提取文本
提取条件匹配的内容

sed 处理文本内容

学习方法:
主要学习选项和条件

语法结构
1.前置命令 | sed 选项 ‘条件指令’
2.sed 选项 ‘条件指令’ 文件

 //1.条件可以是行号或者/正则/
 //2.没有条件时默认所有条件
 //3.指令可以是增删改查等指令
 //4.默认sed会输出所有的内容,可以使用-n  屏蔽输出
 //5.支持扩展正则,使用-r选项

常用的选项:
-n :屏蔽输出
-r :让sed支持扩展正则
-i :直接修改源文件,默认只通过内存进行修改,源文件无影响

    //多个指令用分号,进行间隔

指令:
p : 输出

准备测试文件 cp /etc/passwd ./passwd
1.打印第三行
#sed -n ‘3p’ passwd
2.打印第三到五行
#sed -n ‘3,5p’ passwd
3.打印第三行和第五行:
#sed -n ‘3p;5p’ passwd
4.打印第三行后面的十行
#sed -n ‘3p,+10p’ passwd
5.打印奇数行:
#sed -n ‘1~2p’ passwd
6.打印偶数行:
#sed -n ‘2~2p’ passwd
7.打印包含root的行:
#sed -n ‘/root/p’ passwd
8.打印以bash结尾的行
#sed -n ‘/bash$/p’ passwd

‘$=’ :输出这个文件有多少行


  1. a-z ↩︎

  2. a-zA-Z ↩︎

  3. :lower: ↩︎

  4. :lower: ↩︎

发布了138 篇原创文章 · 获赞 149 · 访问量 8445

猜你喜欢

转载自blog.csdn.net/weixin_44799645/article/details/104898851