shell腳本學習(三)- sed

1.     打印指定行            sed ‘10’p –n 1.txt; sed‘1,4’p –n 1.txt; sed ‘5,$’p –n 1.txt;

2.     打印包含某個行字符串的行   sed –n ‘/root/’p 1.txt 可以使用^ . * $等特殊符號

3.     –e可以實現多個任務同時打印 sed –e ‘/root’p –e ‘/body/’p –n 1.txt

4.     刪除行               sed ‘/root/’d 1.txt ; sed ‘1d’ 1.txt; sed ‘1,10d’1.txt

5.     替換                sed ‘1,2s/ot/to/g’1.txt,其中s就是替換的意思,g爲全局替換

6.     刪除所有數字          sed ‘s/[0-9]//g’ 1.txt

7.     刪除所有非數字         sed ‘s/^[0-9]//g’ 1.txt

8.     直接修改文件內容 sed –i ‘s/ot/to/g’ 1.txt

9.     調換兩個字符串位置      head –n2 1.txt | sed‘s/\(root\)\(.*\)/\(bash\)/3/2/1/’

10.  /etc/passwd複製到/root/test.txt,用sed打印所有行 

                      cp /etc/passwd/root/test.txt && sed ‘1,$’p /root/test.txt

11.  打印test.txt310               sed –n‘3,10’p test.txt

12.  打印test.txt中包含root的行               sed –n ‘/root/’ptest.txt

13.  刪除test.txt15行以及以後的所有行          nl test.txt| sed '15,$'d

14.  刪除包含bash的行                      nl test.txt | sed '/bash/'d

15.  替換test.txtrootbash                nl test.txt | sed ‘s/root/bash/g’

16.  替換test.txt/sbin/nologin/bin/login     

                                nl test.txt | sed 's/\/sbin\/nologin/\/bin\/login/g'

17.  刪除test.txt510行的數字           nl test.txt | sed'5,10s/[0-9]//g'

18.  刪除test.txt中的特殊字符除過數字字母    nl test.txt |sed 's/[^0-9a-zA-Z]//g'

19.  test.txt中第一個單詞和最後一個單詞調換位置              (暫時不會)

20.  test.txt中出現的第一個數字和最後一個單詞替換位置          (暫時不會)

21.  test.txt中第一個數字移動到行末尾                    暫時不會

22.  test.txt20行到末尾行前面加aaa                       (暫時不會)

23.  顯示一下行號                  nl passwd | sed ‘/root/’p –n

24.  對於sed?+就需要脫義符號,否則需要-r參數

25.  這個有點高端了                 headpasswd | sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章