無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          sed用法介紹

          Posted on 2009-11-29 11:54 Gavin.lee 閱讀(305) 評論(0)  編輯  收藏 所屬分類: Linux command

           

          輸出第1行到第3行
          Shell代碼
          1. -bash-3.00$ sed -n '1,3p' sed.txt   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. wangmin803   pts/16       Jul 29 14:18  (219.149.138.142)   
          4. majorchan   pts/21       Jul 29 15:18   (202.203.137.236)  


          輸出第2行
          Shell代碼
          1. -bash-3.00$ sed -n '2p' sed.txt   
          2. wangmin803   pts/16       Jul 29 14:18  (219.149.138.142)  


          查找含有tomotoboy的行讓后輸出
          Shell代碼
          1. -bash-3.00$ sed -n '/tomotoboy/'p sed.txt   
          2. tomotoboy   pts/45       Jul 29 13:53   (219.221.99.155)   
          3. tomotoboy   pts/46       Jul 29 15:24   (219.221.99.155)   
          4. tomotoboy   pts/52       Jul 29 16:20   (219.221.99.155)  


          輸出當前的所有用戶
          Shell代碼
          1. /home/l/g/tomotoboy >who   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)   
          4. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          5. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          6. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          輸出第一行到含有字符串tomotoboy的行
          Shell代碼
          1. /home/l/g/tomotoboy >who | sed -n '1,/tomotoboy/'p   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)  


          輸出原文的同時,輸出tomotoboy所在的行號
          Shell代碼
          1. -/home/l/g/tomotoboy >who|sed '/tomotoboy/'=   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. 2  
          4. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)   
          5. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          6. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          7. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          輸出含有tomotoboy的行的行號
          Shell代碼
          1. /home/l/g/tomotoboy >who | sed -e '/tomotoboy/'=   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. 2  
          4. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)   
          5. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          6. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          7. kindy      pts/38       Aug  7 21:33    (reverse.gdsz.cncnet.net)   
          8. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          輸出第一行到最后一行
          Shell代碼
          1. /home/l/g/tomotoboy >who | sed -n '1,$p'  
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)   
          4. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          5. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          6. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          輸出最后一行
          Shell代碼
          1. /home/l/g/tomotoboy >who|sed -n '$p'  
          2. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          append.sed:sed腳本實現附加功能,將hello tomotoboy附件在tomotoboy所在行之后
          Shell代碼
          1. /home/l/g/tomotoboy >cat append.sed   
          2. #!/bin/sed -f   
          3. /tomotoboy/ a\   
          4. hello tomotoboy   
          5.   
          6. /home/l/g/tomotoboy >who| append.sed   
          7. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          8. tomotoboy   pts/16       Aug  7 21:24   (219.221.98.71)   
          9. hello tomotoboy   
          10. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          11. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          12. kindy      pts/38       Aug  7 21:41    (reverse.gdsz.cncnet.net)   
          13. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          文本輸出時,把tomotoboy替換成hello
          Shell代碼
          1. /home/l/g/tomotoboy >who|sed 's/tomotoboy/hello/'  
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. hello   pts/16       Aug  7 21:24       (219.221.98.71)   
          4. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          5. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          6. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  


          只輸出被替換的行
          Shell代碼
          1. /home/l/g/tomotoboy >who|sed -n 's/tomotoboy/hello/p'  
          2. hello   pts/16       Aug  7 21:24       (219.221.98.71)  


          如果要附加或修改一個字符串,可以使用(&)命令,&命令保存發現模式以便重新調用它,然后把它放在替換字符串里面。
          Shell代碼
          1. /home/l/g/tomotoboy >who|sed -n 's/tomotoboy/hello &/p'  
          2. hello tomotoboy   pts/16       Aug  7 21:24     (219.221.98.71)   
          3. /home/l/g/tomotoboy >who|sed -n 's/tomotoboy/& my friend/p'  
          4. tomotoboy my friend   pts/16       Aug  7 21:24 (219.221.98.71)  


          Shell代碼
          1. 刪除最后一行   
          2. bash-3.00$ sed '$d' sed.txt    
          3. 刪除第一到最后6行   
          4. -bash-3.00$ sed '1,6d' sed.txt    
          5. 刪除第一到最后一行   
          6. -bash-3.00$ sed '1,$d' sed.txt    
          7. 刪除tomotoboy的行   
          8. -bash-3.00$ sed '/tomotoboy/d' sed.txt    
          9. 用seawolf替代tomotoboy   
          10. -bash-3.00$ sed 's/tomotoboy/seawolf/' sed.txt    
          11. 用seawolf替代tomotoboy然后輸出到sed.out   
          12. -bash-3.00$ sed 's/tomotoboy/seawolf/w sed.out' sed.txt  


          看看我們寫好了哪些sed腳本
          Shell代碼
          1. /home/l/g/tomotoboy >ls -al|grep 'sed$'  
          2. -rwxr--r--   1 tomotoboy member        45 Aug  7 21:41 append.sed   
          3. -rwxr--r--   1 tomotoboy member        68 Jul 30 16:28 change.sed  

          看看change.sed的用途是什么?
          Shell代碼
          1. /home/l/g/tomotoboy >cat change.sed   
          2. #!/bin/sed -f   
          3. #change.sed   
          4. /tomotoboy/ c\   
          5. I love China,Ilove Wanzhi.  

          找到tomotoboy,替換為I love China,Ilove Wanzhi.
          Shell代碼
          1. /home/l/g/tomotoboy >who|change.sed   
          2. liuzk423   pts/6        Jul 20 08:27    (219.245.104.240)   
          3. I love China,Ilove Wanzhi.   
          4. guise      pts/21       Aug  7 17:56    (124.76.10.207)   
          5. guise      pts/35       Aug  7 21:13    (58.41.162.27)   
          6. uyty       pts/38       Aug  7 22:09    (p3213-ipbf803souka.saitama.ocn.ne.jp)   
          7. yagamil    pts/46       Aug  7 20:48    (199.40.206.191)  
          主站蜘蛛池模板: 开远市| 平江县| 宁安市| 蕉岭县| 揭东县| 抚州市| 孟村| 古丈县| 阳泉市| 龙川县| 上林县| 宜宾县| 家居| 扶余县| 昌图县| 凤台县| 乌拉特前旗| 象州县| 深圳市| 义乌市| 常山县| 中宁县| 黄陵县| 湖口县| 屏东县| 崇义县| 栖霞市| 海原县| 永定县| 普安县| 大丰市| 永善县| 乌海市| 贵港市| 太保市| 怀柔区| 长汀县| 宁波市| 安义县| 潜山县| 营口市|