[root@shell ~/sedawk]# sed -e '/1/s/^/#/' -e /3/s/^/#/ -e '/3/a aa1\naa2\naa3' text.txt
[root@shell ~/sedawk]# sed -e '/1/s/dd/\#dd/' -e '/3/s/dd/\#dd/' -e '/4/iaa1\naa2\naa3' cto.txt
練習二:
經過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