分享

sed用法介绍 - Stephen,Yang - JavaEye技术网站

 昵称370235 2009-10-20
输出第1行到第3行
Shell代码 复制代码
  1. -bash-3.00sed -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.00sed -n '2p' sed.txt   
  2. wangmin803   pts/16       Jul 29 14:18  (219.149.138.142)  


查找含有tomotoboy的行让后输出
Shell代码 复制代码
  1. -bash-3.00sed -n '/tomotoboy/'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.sedsed脚本实现附加功能,将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.00sed '$d' sed.txt    
  3. 删除第一到最后6行   
  4. -bash-3.00sed '1,6d' sed.txt    
  5. 删除第一到最后一行   
  6. -bash-3.00sed '1,$d' sed.txt    
  7. 删除tomotoboy的行   
  8. -bash-3.00sed '/tomotoboy/d' sed.txt    
  9. 用seawolf替代tomotoboy   
  10. -bash-3.00sed 's/tomotoboy/seawolf/' sed.txt    
  11. 用seawolf替代tomotoboy然后输出到sed.out   
  12. -bash-3.00sed '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)  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多