以下练习题目来源于互联网:
练习一:
a
b
c
dd xxxxx1
dd xxxxxxxx2
dd xxxxxxxxx3
dd xxxxxxxx4
写个shell脚本,搜索到以dd开头的第1行和第3行将其前面加个#注释,并在第3行后面添加3行aa1,aa2,aa3....
方法一:
[root@shell ~/sedawk]# sed -e '/1/s/^/#/' -e /3/s/^/#/ -e '/3/a aa1\naa2\naa3' text.txt
a
b
c
#dd xxxxx1
dd xxxxxxxx2
#dd xxxxxxxxx3
aa1
aa2
aa3
dd xxxxxxxx4
方法二:
[root@shell ~/sedawk]# sed -e '/1/s/dd/\#dd/' -e '/3/s/dd/\#dd/' -e '/4/iaa1\naa2\naa3' cto.txt
a
b
c
#dd xxxxx1
dd xxxxxxxx2
#dd xxxxxxxxx3
aa1
aa2
aa3
dd xxxxxxxx4
练习二:
通过Apache访问日志access.log 统计IP和每个地址访问的次数,按从大到小前10名
[root@shell ~/sedawk]# cat httpd.log | awk '{print $1}' | sort | uniq -c | sort -r
375 192.168.209.129
270 192.168.209.1
154 192.168.209.130
练习三:
如有一文件,test.txt,内容如下:
http://www.5566.cn/produce/20070804112/315613171.shtml
http://bj.5566.cn/produce/20070804112/31581217.shtml
http://sz.5566.cn/produce/2008090412/31581247.shtml
要求如下:将http://*.5566.cn/替换成/home/html/www.5566.cn,要求用sed
[root@shell ~/sedawk]# sed -n 's/http.*\.cn/home\/html\/www.5566.cn/gp' url.txt
home/html/www.5566.cn/produce/20070804112/315613171.shtml
home/html/www.5566.cn/produce/20070804112/31581217.shtml
home/html/www.5566.cn/produce/2008090412/31581247.shtml
练习四:
[root@shell ~/sedawk]# cat ip.txt
192.168.1.2/24
167.178.1.3/24
212.121.1.3/24
192.168.1.2/24
167.178.1.3/24
212.121.1.3/24
123.124.122.121/24
123.124.122.121/24
编写脚本将其前面添加入“abcd”并将 /去掉然后以排序汇总
[root@shell ~/sedawk]# sed 's#/# #g' ip.txt|sed 's/^/abcd/g'|sort -r|uniq -c
2 abcd212.121.1.3 24
2 abcd192.168.1.2 24
2 abcd167.178.1.3 24
2 abcd123.124.122.121 24