三劍客之sed經常使用操做

Sed
Sed是一個強大的文本處理工具
能夠採用正則匹配,對文本進行插入刪除修改等操做
Sed處理的時候,一次處理一行,每一次把當前處理的存放在臨時緩衝區,處理完後輸出緩衝區內容到屏幕,而後把下一行讀入緩衝區,如此重複,直到結尾。


一、命令格式和參數
sed [-nefr] [動做] 文件
參數:
-n 安靜模式,在sed處理的時候,全部來自STDIN的數據都會被輸出到終端,加上-n會只輸出處理的哪行
-e 直接在命令列上進行sed動做編輯
-f 直接將sed的動做寫在文件內
-r sed動做支持延伸的正則表達(默認只是基礎正則)
-i 直接修改文件內容(慎用,尤爲是用系統文件作練習的時候)


動做:
append:增長,在當前行的下一行增長
c   :取代,取代n1到n2之間的行
d delete:刪除
i 插入,目前行的上一行插入
p 打印,經常與-n使用
s 取代,s/old/new/g


二、基礎用法詳解
(1)第一行以後添加一行
app

  1. [root@localhost ~]# nl file.txt | sed "1a add text"  ide

  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  工具

  3. add text  this

  4.      2  192.168.0.1  url

  5.      3  162.12.0.123  spa

  6.      4  this is the last line  .net

(2)第一行以前添加一行 copy3d

  1. [root@localhost ~]# nl file.txt | sed "1i add text"  code

  2. add text  orm

  3.      1  wtmp begins Mon Feb 24 14:26:08 2014  

  4.      2  192.168.0.1  

  5.      3  162.12.0.123  

  6.      4  this is the last line  

(3)刪除第2,3行

  1. [root@localhost ~]# nl file.txt | sed "2,3d"  

  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  

  3.      4  this is the last line  

(4)打印第2,3行 copy

  1. [root@localhost ~]# sed -n "2,3p" file.txt   

  2. 192.168.0.1  

  3. 162.12.0.123  


這裏要提到的是,儘可能使用-n,否則會出現這樣的結果 copy

  1. [root@localhost ~]# sed "2,3p" file.txt   

  2. wtmp begins Mon Feb 24 14:26:08 2014  

  3. 192.168.0.1  

  4. 192.168.0.1  

  5. 162.12.0.123  

  6. 162.12.0.123  

  7. this is the last line  


(5)把168換成169
先看源文件 copy

  1. [root@localhost ~]# cat file.txt   

  2. wtmp begins Mon Feb 24 14:26:08 2014  

  3. 192.168.0.1  

  4. 162.12.0.123  

  5. this is the last line  

處理後 copy

  1. [root@localhost ~]# sed "s/168/169/g" file.txt   

  2. wtmp begins Mon Feb 24 14:26:08 2014  

  3. 192.169.0.1  

  4. 162.12.0.123  

  5. this is the last line  


(6)插入多行

  1. [root@localhost ~]# nl file.txt | sed "2afirst\nsecond" file.txt   

  2. wtmp begins Mon Feb 24 14:26:08 2014  

  3. 192.168.0.1  

  4. first  

  5. second  

  6. 162.12.0.123  

  7. this is the last line  


(7)匹配數據,而後進行操做
只須要在上述的基礎上加上正則匹配
sed "/匹配的模式/處理的方式" file.txt 
sed "/^root/d" file.txt 對開始有root的刪除
例如
匹配begin,並刪除改行 copy

  1. [root@localhost ~]# nl file.txt | sed "/begin/d"  

  2.      2  192.168.0.1  

  3.      3  162.12.0.123  

  4.      4  this is the last line  

匹配123,而且把含有123的行162都替換成172 copy

  1. [root@localhost ~]# nl file.txt | sed "/123/{s/162/172/g;q}"  

  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  

  3.      2  192.168.0.1  

  4.      3  172.12.0.123  

  5.      4  this is the last line  

這裏大括號{}裏能夠執行多個命令,用;隔開便可,q是退出
(8)連續編輯 -e
刪除第二行,而且匹配把last替換成new

  1. <pre name="code" class="plain">[root@localhost ~]# nl file.txt | sed -e "2d" -e "s/last/new/"  

  2.      1  wtmp begins Mon Feb 24 14:26:08 2014  

  3.      3  162.12.0.123  

  4.      4  this is the new line  



(9)直接修改文件,切記不要修改系統文件 copy

  1. [root@localhost ~]# sed -i "/begin/{s/24/25/g}" file.txt   

  2. [root@localhost ~]# cat file.txt   

  3. wtmp begins Mon Feb 25 14:26:08 2014  

  4. 192.168.0.1  

  5. 162.12.0.123  

  6. this is the last line  



三 、一個比較有趣的例子
如何替換\n也就是把全部的行都歸爲一行

第一種方式 copy

  1. [root@localhost ~]# sed ':a;N;$!ba;s/\n/ /g' file.txt   

  2. wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line  


第二種方式opy

  1. [root@localhost ~]# tr "\n" " " < file.txt   

  2. wtmp begins Mon Feb 25 14:26:08 2014 192.168.0.1 162.12.0.123 this is the last line last linen  

相關文章
相關標籤/搜索