<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    sed用法介紹

    Posted on 2009-11-29 11:54 Gavin.lee 閱讀(300) 評論(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)  
    主站蜘蛛池模板: 中文字幕天天躁日日躁狠狠躁免费| 免费毛片毛片网址| **真实毛片免费观看| 在线观看亚洲av每日更新| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久亚洲精品11p| 国产无遮挡吃胸膜奶免费看视频| 亚洲欧洲国产综合| 无码av免费网站| 久久亚洲精精品中文字幕| 99精品视频免费观看| 亚洲综合图片小说区热久久| 精品一区二区三区免费毛片爱| 亚洲视频一区在线观看| 黄色永久免费网站| 亚洲成av人无码亚洲成av人| 全部免费a级毛片| 中国一级全黄的免费观看| 亚洲国产综合精品中文第一区| 很黄很色很刺激的视频免费| 亚洲国产无线乱码在线观看| mm1313亚洲精品无码又大又粗| 男女一进一出抽搐免费视频| 亚洲国产一区二区a毛片| 国产在线观看片a免费观看| 麻豆91免费视频| 亚洲午夜久久久久妓女影院| 免费A级毛片在线播放| 亚洲国产日韩精品| 亚洲精品亚洲人成在线观看下载 | 98精品全国免费观看视频| 亚洲人成网站看在线播放| 国产一级做a爱免费视频| 中文永久免费观看网站| 99人中文字幕亚洲区| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 在线观看亚洲AV日韩AV| 亚洲午夜精品久久久久久浪潮| 免费国产99久久久香蕉| 亚洲国产午夜精品理论片在线播放 | 国产亚洲精品自在线观看|