sed用法及举例
基本sed編程舉例
-n 只輸出處理后的行
-i 直接修改讀取的文件內容,而不是輸出到終端
-r 使用擴展正則表達式
打印指定行
[root@tyzZ SED]# cat file xdtn 6/56 12356 G 8 34 78 zdyn 6/56 12356 G 8 34 78 aklm 6/56 12356 G 8 34 78 xcvg 6/56 12356 G 8 34 78 uiol 6/56 12356 G 8 34 78 bghj 6/56 12356 G 8 34 78 asd 123 $q12 F 9 qwe dfgg zxce 6/56 12356 G 8 34 78 [root@tyzZ SED]# sed -n 2p file zdyn 6/56 12356 G 8 34 78 [root@tyzZ SED]# sed -n "2,4"p file zdyn 6/56 12356 G 8 34 78 aklm 6/56 12356 G 8 34 78 xcvg 6/56 12356 G 8 34 78打印匹配指定字母或單詞的行
[root@tyzZ SED]# sed -n '/\$/'p file #匹配元字符 asd 123 $q12 F 9 qwe dfgg [root@tyzZ SED]# sed -n '/aklm/'p file aklm 6/56 12356 G 8 34 78顯示所有行
[root@tyzZ SED]# sed -n '1,$p' file #$意思是最后一行 xdtn 6/56 12356 G 8 34 78 zdyn 6/56 12356 G 8 34 78 aklm 6/56 12356 G 8 34 78 xcvg 6/56 12356 G 8 34 78 uiol 6/56 12356 G 8 34 78 bghj 6/56 12356 G 8 34 78 asd 123 $q12 F 9 qwe dfgg zxce 6/56 12356 G 8 34 78顯示第一行
[root@tyzZ SED]# sed -n '1p' file xdtn 6/56 12356 G 8 34 78顯示最后一行
[root@tyzZ SED]# sed -n '$p' file zxce 6/56 12356 G 8 34 78打印行號
使用“=”
[root@tyzZ SED]# sed -n '/uiol/=' file 5如果打印行號及匹配行的話 需要兩條sed命令
刪除文本
[root@tyzZ SED]# sed '1d' file zdyn 6/56 12356 G 8 34 78 aklm 6/56 12356 G 8 34 78 xcvg 6/56 12356 G 8 34 78 uiol 6/56 12356 G 8 34 78 bghj 6/56 12356 G 8 34 78 asd 123 $q12 F 9 qwe dfgg zxce 6/56 12356 G 8 34 78刪除其他與顯示其他 規則相同
替換文本
[root@tyzZ SED]# sed 's/xdtn/XDTN/' file #將文件中所有的xdtn替換為XDTN刪除文本
[root@tyzZ SED]# sed 's/\$//' file #將文本中所有的$刪去全局替換
[root@tyzZ SED]# sed 's/78/bb/g' file將替換后的結果 寫入到文件
[root@tyzZ SED]# sed 's/78/bb/w asd' file XDTN 6/56 12356 G 8 34 bb zdyn 6/56 12356 G 8 34 bb aklm 6/56 12356 G 8 34 bb xcvg 6/56 12356 G 8 $34 bb uiol 6/56 $12356 G 8 34 bb bghj 6/56 12356 G 8 34 bb asd 123 $q12 F 9 qwe dfgg $zxce 6/56 12356 G 8 34 bb[root@tyzZ SED]# cat asd XDTN 6/56 12356 G 8 34 bb zdyn 6/56 12356 G 8 34 bb aklm 6/56 12356 G 8 34 bb xcvg 6/56 12356 G 8 $34 bb uiol 6/56 $12356 G 8 34 bb bghj 6/56 12356 G 8 34 bb $zxce 6/56 12356 G 8 34 bbsed中,使用\u表示大寫,\l表示小寫
打印指定行數 (^開頭)(結尾$)
[root@tyzz ~]# sed -n '1,10'p 1.txt root:x:0:0:root:/root:/bin/bash 123131231313123123131231313453453665789708 !@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$ bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/syncrr匹配指定關鍵字
[root@tyzz ~]# sed -n '/root/'p 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin刪除匹配到的行。。。(d=delete)
[root@tyzz ~]# sed '/[0-9]/'d 1.txt !@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$rr rrr rrrr刪除空行
[root@tyzz ~]# sed '/^$/'d 1.txt [root@tyzz ~]# sed '/^$/'d 3.txt i adsads替換
[root@tyzz ~]# sed 's/nologin/aaaaa/g' 1.txt root:x:0:0:root:/root:/bin/bash 123131231313123123131231313453453665789708 !@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$ bin:x:1:1:bin:/bin:/sbin/aaaaarr rrr rrrr shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/aaaaa operator:x:11:0:operator:/root:/sbin/aaaaaftp:x:14:50:FTP User:/var/ftp:/sbin/aaaaa nobody:x:99:99:Nobody:/:/sbin/aaaaadbus:x:81:81:System message bus:/:/sbin/aaaaa polkitd:x:999:999:User for polkitd:/:/sbin/aaaaantp:x:38:38::/etc/ntp:/sbin/aaaaa avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/aaaaa avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/aaaaa libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/aaaaa將數字去掉(將數字替換為空)
[root@tyzz ~]# sed 's/[0-9]//g' 1.txt root:x:::root:/root:/bin/bash!@#!@#@$@$#%$%^%^&^*^(*)()**))^%^#$@$ bin:x:::bin:/bin:/sbin/nologinrr rrr rrrr shutdown:x:::shutdown:/sbin:/sbin/shutdown去掉所有大小寫字母
sed 's#[a-zA-z]##g' 1.txt ::0:0::/:// 123131231313123123131231313453453665789708 !@#!@#@$@$#%$%%&*(*)()**))%#$@$ ::1:1::/://::6:0::/://所有小寫字母 替換為大寫
sed 's/[a-z]/\u&/g' 1.txt大寫字母 替換為小寫
sed 's/[A-Z]/\l&/g' 1.txt把每個單詞的第一個小寫字母變大寫
[root@tyzz ~]# sed 's/\b[a-z]/\u&/g' 3.txt Asdada Asdadas Zxczxc Ffghf Asdasd Adad Cbbcvbghf Reert Hfh Vbnvnjghj I Adsads轉載于:https://www.cnblogs.com/aallenn/p/6700587.html
總結
- 上一篇: Omi框架学习之旅 - 通过对象实例来实
- 下一篇: 任务03——简单程序测试及 GitHub