第十一章:正则表达式课后习题
一、sed练习题
1.把/etc/passwd 复制到/root/test.txt,用sed打印所有行。
[root@zl_cloud ~]# cp /etc/passwd /root/test.txt
[root@zl_cloud ~]# sed -n '1,$'p test.txt
[root@zl_cloud ~]#
2.打印test.txt的第3行~第10行。
# sed -n ‘3,10’p test.txt
3.打印test.txt中包含root的行。
# sed -n ‘/root/’p test.txt
4.删除test.txt的第15行以及后面的所有行。
# sed ‘15,$’d test.txt
5.删除test.txt中包含bash的行。
# sed ‘/bash/’d test.txt
6.将 test.txt 中的 root 替换为 toor。
# sed ‘s/root/toor/g’ test.txt
7.将 test.txt 中的 /sbin/nologin 替换为 /bin/login
# sed ‘s#sbin/nologin#bin/login#g’ test.txt
8.删除test.txt第5行~第10行中所有的数字。
# sed ‘5,10s/[0-9]*//g’ test.txt
9.删除test.txt中所有的特殊字符(除了数字以及大小写字母)。
# sed ‘1,$s/[^0-9a-zA-Z]//g’ test.txt
10.把test.txt中第一个単词和最后一个单词调换位置。
# sed -r 's/(^[a-zA-Z]*)([^a-zA-Z].*[^a-zA-Z])([a-zA-Z]*$)/\3\2\1/' te