找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4564|回复: 0

Linux Shell常用技巧(三)

[复制链接]
发表于 2012-4-21 10:16:33 | 显示全部楼层 |阅读模式
  1. 八. 流编辑器sed:
  2.     sed一次处理一行文件并把输出送往屏幕。sed把当前处理的行存储在临时缓冲区中,称为模式空间(pattern space)。一旦sed完成对模式空间中的行的处理,模式空间中的行就被送往屏幕。行被处理完成之后,就被移出模式空间,程序接着读入下一行,处理,显示,移出......文件输入的最后一行被处理完以后sed结束。通过存储每一行在临时缓冲区,然后在缓冲区中操作该行,保证了原始文件不会被破坏。
  3.    
  4.     1.  sed的命令和选项:
  5. 命令        功能描述
  6. a\         在当前行的后面加入一行或者文本。
  7. c\         用新的文本改变或者替代本行的文本。
  8. d         从pattern space位置删除行。
  9. i\         在当前行的上面插入文本。
  10. h         拷贝pattern space的内容到holding buffer(特殊缓冲区)。
  11. H         追加pattern space的内容到holding buffer。
  12. g         获得holding buffer中的内容,并替代当前pattern space中的文本。
  13. G         获得holding buffer中的内容,并追加到当前pattern space的后面。
  14. n         读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
  15. p         打印pattern space中的行。
  16. P         打印pattern space中的第一行。
  17. q         退出sed。
  18. w file         写并追加pattern space到file的末尾。
  19. !         表示后面的命令对所有没有被选定的行发生作用。
  20. s/re/string         用string替换正则表达式re。
  21. =         打印当前行号码。
  22. 替换标记         
  23. g         行内全面替换,如果没有g,只替换第一个匹配。
  24. p         打印行。
  25. x         互换pattern space和holding buffer中的文本。
  26. y         把一个字符翻译为另一个字符(但是不能用于正则表达式)。
  27. 选项         
  28. -e         允许多点编辑。
  29. -n         取消默认输出。
  30.      需要说明的是,sed中的正则和grep的基本相同,完全可以参照本系列的第一篇中的详细说明。
  31.    2.  sed实例:
  32.     /> cat testfile
  33.     northwest       NW     Charles Main           3.0      .98      3       34
  34.     western          WE      Sharon Gray           5.3      .97     5       23
  35.     southwest       SW     Lewis Dalsass          2.7      .8      2       18
  36.     southern         SO      Suan Chin               5.1     .95     4       15
  37.     southeast       SE       Patricia Hemenway   4.0      .7      4       17
  38.     eastern           EA      TB Savage               4.4     .84     5       20
  39.     northeast        NE      AM Main Jr.              5.1     .94     3       13
  40.     north              NO      Margot Weber         4.5     .89     5       9
  41.     central            CT      Ann Stephens          5.7     .94     5       13
  42.     /> sed '/north/p' testfile #如果模板north被找到,sed除了打印所有行之外,还有打印匹配行。
  43.     northwest       NW      Charles Main           3.0     .98     3       34
  44.     northwest       NW      Charles Main           3.0     .98     3       34
  45.     western          WE      Sharon Gray           5.3     .97     5       23
  46.     southwest      SW      Lewis Dalsass          2.7     .8       2       18
  47.     southern        SO       Suan Chin               5.1     .95     4       15
  48.     southeast       SE       Patricia Hemenway   4.0     .7       4       17
  49.     eastern           EA      TB Savage               4.4     .84     5       20
  50.     northeast        NE      AM Main Jr.              5.1     .94     3       13
  51.     northeast        NE      AM Main Jr.              5.1     .94     3       13
  52.     north              NO      Margot Weber         4.5     .89     5       9
  53.     north              NO      Margot Weber         4.5     .89     5       9
  54.     central            CT      Ann Stephens          5.7     .94     5       13
  55.     #-n选项取消了sed的默认行为。在没有-n的时候,包含模板的行被打印两次,但是在使用-n的时候将只打印包含模板的行。
  56.     /> sed -n '/north/p' testfile
  57.     northwest       NW      Charles Main    3.0     .98     3       34
  58.     northeast        NE      AM Main Jr.       5.1     .94     3       13
  59.     north              NO      Margot Weber  4.5     .89     5       9
  60.     /> sed '3d' testfile  #第三行被删除,其他行默认输出到屏幕。
  61.     northwest       NW     Charles Main            3.0     .98     3       34
  62.     western          WE      Sharon Gray           5.3     .97     5       23
  63.     southern         SO      Suan Chin               5.1     .95     4       15
  64.     southeast       SE       Patricia Hemenway   4.0     .7       4       17
  65.     eastern           EA      TB Savage               4.4     .84     5       20
  66.     northeast       NE       AM Main Jr.              5.1     .94     3       13
  67.     north             NO       Margot Weber         4.5     .89     5       9
  68.     central           CT       Ann Stephens          5.7     .94     5       13
  69.     /> sed '3,$d' testfile  #从第三行删除到最后一行,其他行被打印。$表示最后一行。
  70.     northwest       NW      Charles Main    3.0     .98     3       34
  71.     western          WE      Sharon Gray    5.3     .97     5       23
  72.     /> sed '$d' testfile    #删除最后一行,其他行打印。
  73.     northwest       NW     Charles Main           3.0     .98     3       34
  74.     western          WE     Sharon Gray           5.3     .97     5       23
  75.     southwest       SW    Lewis Dalsass          2.7     .8      2       18
  76.     southern         SO     Suan Chin              5.1     .95     4       15
  77.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  78.     eastern           EA      TB Savage             4.4     .84     5       20
  79.     northeast       NE      AM Main Jr.             5.1     .94     3       13
  80.     north             NO      Margot Weber        4.5     .89     5       9
  81.     /> sed '/north/d' testfile #删除所有包含north的行,其他行打印。
  82.     western           WE      Sharon Gray           5.3     .97     5       23
  83.     southwest       SW      Lewis Dalsass          2.7     .8      2       18
  84.     southern          SO      Suan Chin               5.1     .95     4       15
  85.     southeast         SE      Patricia Hemenway   4.0     .7       4       17
  86.     eastern            EA      TB Savage               4.4     .84     5       20
  87.     central             CT      Ann Stephens          5.7     .94     5       13
  88.     #s表示替换,g表示命令作用于整个当前行。如果该行存在多个west,都将被替换为north,如果没有g,则只是替换第一个匹配。
  89.     /> sed 's/west/north/g' testfile
  90.     northnorth      NW     Charles Main           3.0     .98    3       34
  91.     northern         WE      Sharon Gray          5.3     .97    5       23
  92.     southnorth      SW     Lewis Dalsass         2.7     .8      2       18
  93.     southern         SO      Suan Chin              5.1     .95    4       15
  94.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  95.     eastern           EA      TB Savage             4.4     .84     5       20
  96.     northeast       NE      AM Main Jr.              5.1     .94    3       13
  97.     north             NO      Margot Weber        4.5     .89     5       9
  98.     central            CT      Ann Stephens        5.7     .94     5       13
  99.     /> sed -n 's/^west/north/p' testfile #-n表示只打印匹配行,如果某一行的开头是west,则替换为north。
  100.     northern        WE      Sharon Gray     5.3     .97     5       23
  101.     #&符号表示替换字符串中被找到的部分。所有以两个数字结束的行,最后的数字都将被它们自己替换,同时追加.5。
  102.     /> sed 's/[0-9][0-9]$/&.5/' testfile
  103.     northwest       NW      Charles Main          3.0     .98     3       34.5
  104.     western          WE      Sharon Gray           5.3     .97     5       23.5
  105.     southwest       SW      Lewis Dalsass        2.7     .8       2       18.5
  106.     southern         SO      Suan Chin              5.1     .95     4       15.5
  107.     southeast       SE      Patricia Hemenway   4.0     .7       4       17.5
  108.     eastern           EA      TB Savage              4.4     .84     5       20.5
  109.     northeast        NE      AM Main Jr.             5.1     .94     3       13.5
  110.     north              NO      Margot Weber        4.5     .89     5       9
  111.     central            CT      Ann Stephens         5.7     .94     5       13.5
  112.     /> sed -n 's/Hemenway/Jones/gp' testfile  #所有的Hemenway被替换为Jones。-n选项加p命令则表示只打印匹配行。
  113.     southeast       SE      Patricia Jones  4.0     .7      4       17
  114.     #模板Mar被包含在一对括号中,并在特殊的寄存器中保存为tag 1,它将在后面作为\1替换字符串,Margot被替换为Marlianne。
  115.     /> sed -n 's/\(Mar\)got/\1lianne/p' testfile
  116.     north           NO      Marlianne Weber 4.5     .89     5       9
  117.     #s后面的字符一定是分隔搜索字符串和替换字符串的分隔符,默认为斜杠,但是在s命令使用的情况下可以改变。不论什么字符紧跟着s命令都认为是新的分隔符。这个技术在搜索含斜杠的模板时非常有用,例如搜索时间和路径的时候。
  118.     /> sed 's#3#88#g' testfile
  119.     northwest       NW      Charles Main            88.0    .98     88     884
  120.     western          WE       Sharon Gray           5.88    .97     5       288
  121.     southwest       SW      Lewis Dalsass          2.7     .8       2       18
  122.     southern         SO       Suan Chin               5.1     .95     4       15
  123.     southeast       SE        Patricia Hemenway   4.0     .7        4       17
  124.     eastern           EA       TB Savage               4.4     .84      5      20
  125.     northeast        NE       AM Main Jr.              5.1     .94      88     188
  126.     north              NO       Margot Weber         4.5     .89      5       9
  127.     central            CT       Ann Stephens          5.7     .94      5       188
  128.     #所有在模板west和east所确定的范围内的行都被打印,如果west出现在esst后面的行中,从west开始到下一个east,无论这个east出现在哪里,二者之间的行都被打印,即使从west开始到文件的末尾还没有出现east,那么从west到末尾的所有行都将打印。
  129.     /> sed -n '/west/,/east/p' testfile
  130.     northwest       NW      Charles Main           3.0     .98      3      34
  131.     western          WE      Sharon Gray            5.3     .97     5      23
  132.     southwest       SW     Lewis Dalsass          2.7     .8       2      18
  133.     southern         SO      Suan Chin               5.1     .95     4      15
  134.     southeast        SE      Patricia Hemenway    4.0     .7       4      17
  135.     /> sed -n '5,/^northeast/p' testfile  #打印从第五行开始到第一个以northeast开头的行之间的所有行。
  136.     southeast       SE      Patricia Hemenway   4.0     .7       4       17
  137.     eastern           EA      TB Savage              4.4     .84     5       20
  138.     northeast        NE      AM Main Jr.             5.1     .94     3       13
  139.     #-e选项表示多点编辑。第一个编辑命令是删除第一到第三行。第二个编辑命令是用Jones替换Hemenway。
  140.     /> sed -e '1,3d' -e 's/Hemenway/Jones/' testfile
  141.     southern        SO      Suan Chin          5.1     .95     4       15
  142.     southeast       SE      Patricia Jones      4.0     .7      4       17
  143.     eastern          EA      TB Savage          4.4     .84     5       20
  144.     northeast       NE      AM Main Jr.         5.1     .94     3       13
  145.     north             NO      Margot Weber    4.5     .89     5       9
  146.     central           CT      Ann Stephens     5.7     .94     5       13
  147.     /> sed -n '/north/w newfile' testfile #将所有匹配含有north的行写入newfile中。
  148.     /> cat newfile
  149.     northwest       NW      Charles Main     3.0     .98     3       34
  150.     northeast       NE      AM Main Jr.         5.1     .94     3       13
  151.     north             NO      Margot Weber    4.5     .89     5       9
  152.     /> sed '/eastern/i\ NEW ENGLAND REGION' testfile #i是插入命令,在匹配模式行前插入文本。
  153.     northwest       NW      Charles Main          3.0     .98      3       34
  154.     western          WE      Sharon Gray           5.3     .97     5       23
  155.     southwest       SW      Lewis Dalsass         2.7     .8      2       18
  156.     southern         SO      Suan Chin              5.1     .95     4       15
  157.     southeast        SE      Patricia Hemenway   4.0     .7      4       17
  158.     NEW ENGLAND REGION
  159.     eastern          EA      TB Savage              4.4     .84     5       20
  160.     northeast       NE      AM Main Jr.             5.1     .94     3       13
  161.     north             NO      Margot Weber        4.5     .89     5       9
  162.     central           CT      Ann Stephens         5.7     .94     5       13
  163.     #找到匹配模式eastern的行后,执行后面花括号中的一组命令,每个命令之间用逗号分隔,n表示定位到匹配行的下一行,s/AM/Archie/完成Archie到AM的替换,p和-n选项的合用,则只是打印作用到的行。
  164.     /> sed -n '/eastern/{n;s/AM/Archie/;p}' testfile
  165.     northeast       NE      Archie Main Jr. 5.1     .94     3       13
  166.     #-e表示多点编辑,第一个编辑命令y将前三行中的所有小写字母替换为大写字母,-n表示不显示替换后的输出,第二个编辑命令将只是打印输出转换后的前三行。注意y不能用于正则。
  167.     /> sed -n -e '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' -e '1,3p' testfile
  168.     NORTHWEST       NW      CHARLES MAIN     3.0     .98     3       34
  169.     WESTERN           WE      SHARON GRAY      5.3     .97     5       23
  170.     SOUTHWEST       SW      LEWIS DALSASS   2.7     .8      2       18
  171.     /> sed '2q' testfile  #打印完第二行后退出。
  172.     northwest       NW      Charles Main    3.0     .98     3       34
  173.     western          WE      Sharon Gray     5.3     .97     5       23
  174.     #当模板Lewis在某一行被匹配,替换命令首先将Lewis替换为Joseph,然后再用q退出sed。
  175.      /> sed '/Lewis/{s/Lewis/Joseph/;q;}' testfile
  176.     northwest       NW      Charles Main      3.0     .98     3       34
  177.     western          WE      Sharon Gray      5.3     .97     5       23
  178.     southwest       SW      Joseph Dalsass  2.7     .8      2       18
  179.     #在sed处理文件的时候,每一行都被保存在pattern space的临时缓冲区中。除非行被删除或者输出被取消,否则所有被处理过的行都将打印在屏幕上。接着pattern space被清空,并存入新的一行等待处理。在下面的例子中,包含模板的northeast行被找到,并被放入pattern space中,h命令将其复制并存入一个称为holding buffer的特殊缓冲区内。在第二个sed编辑命令中,当达到最后一行后,G命令告诉sed从holding buffer中取得该行,然后把它放回到pattern space中,且追加到现在已经存在于模式空间的行的末尾。
  180.      /> sed -e '/northeast/h' -e '$G' testfile
  181.     northwest       NW     Charles Main            3.0    .98     3       34
  182.     western          WE     Sharon Gray            5.3    .97     5       23
  183.     southwest       SW    Lewis Dalsass          2.7     .8       2       18
  184.     southern         SO     Suan Chin               5.1     .95     4       15
  185.     southeast       SE      Patricia Hemenway   4.0     .7       4       17
  186.     eastern           EA      TB Savage              4.4     .84     5       20
  187.     northeast       NE      AM Main Jr.              5.1     .94     3       13
  188.     north             NO      Margot Weber         4.5     .89     5       9
  189.     central           CT      Ann Stephens          5.7     .94     5       13
  190.     northeast       NE      AM Main Jr.              5.1     .94     3       13
  191.     #如果模板WE在某一行被匹配,h命令将使得该行从pattern space中复制到holding buffer中,d命令在将该行删除,因此WE匹配行没有在原来的位置被输出。第二个命令搜索CT,一旦被找到,G命令将从holding buffer中取回行,并追加到当前pattern space的行末尾。简单的说,WE所在的行被移动并追加到包含CT行的后面。
  192.     /> sed -e '/WE/{h;d;}' -e '/CT/{G;}' testfile
  193.     northwest       NW    Charles Main           3.0     .98     3       34
  194.     southwest       SW    Lewis Dalsass         2.7     .8      2       18
  195.     southern         SO     Suan Chin              5.1     .95     4       15
  196.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  197.     eastern           EA     TB Savage              4.4     .84     5       20
  198.     northeast       NE      AM Main Jr.              5.1     .94     3       13
  199.     north             NO      Margot Weber         4.5     .89     5       9
  200.     central           CT      Ann Stephens          5.7     .94     5       13
  201.     western         WE      Sharon Gray           5.3     .97     5       23
  202.     #第一个命令将匹配northeast的行从pattern space复制到holding buffer,第二个命令在读取的文件的末尾时,g命令告诉sed从holding buffer中取得行,并把它放回到pattern space中,以替换已经存在于pattern space中的。简单说就是包含模板northeast的行被复制并覆盖了文件的末尾行。
  203.     /> sed -e '/northeast/h' -e '$g' testfile
  204.     northwest       NW     Charles Main          3.0     .98     3       34
  205.     western          WE      Sharon Gray         5.3     .97      5       23
  206.     southwest       SW     Lewis Dalsass        2.7     .8       2       18
  207.     southern         SO      Suan Chin             5.1     .95     4       15
  208.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  209.     eastern           EA      TB Savage             4.4     .84     5       20
  210.     northeast       NE      AM Main Jr.             5.1     .94     3       13
  211.     north             NO      Margot Weber        4.5     .89     5       9
  212.     northeast       NE      AM Main Jr.             5.1     .94     3       13
  213.     #模板WE匹配的行被h命令复制到holding buffer,再被d命令删除。结果可以看出WE的原有位置没有输出。第二个编辑命令将找到匹配CT的行,g命令将取得holding buffer中的行,并覆盖当前pattern space中的行,即匹配CT的行。简单的说,任何包含模板northeast的行都将被复制,并覆盖包含CT的行。   
  214.     /> sed -e '/WE/{h;d;}' -e '/CT/{g;}' testfile
  215.     northwest       NW    Charles Main           3.0     .98      3      34
  216.     southwest       SW    Lewis Dalsass         2.7     .8       2       18
  217.     southern         SO     Suan Chin              5.1     .95      4      15
  218.     southeast       SE      Patricia Hemenway   4.0     .7       4      17
  219.     eastern          EA      TB Savage              4.4     .84      5      20
  220.     northeast       NE      AM Main Jr.              5.1     .94     3      13
  221.     north             NO      Margot Weber        4.5     .89      5      9
  222.     western         WE      Sharon Gray           5.3     .97     5      23
  223.     #第一个编辑中的h命令将匹配Patricia的行复制到holding buffer中,第二个编辑中的x命令,会将holding buffer中的文本考虑到pattern space中,而pattern space中的文本被复制到holding buffer中。因此在打印匹配Margot行的地方打印了holding buffer中的文本,即第一个命令中匹配Patricia的行文本,第三个编辑命令会将交互后的holding buffer中的文本在最后一行的后面打印出来。
  224.      /> sed -e '/Patricia/h' -e '/Margot/x' -e '$G' testfile
  225.     northwest       NW      Charles Main           3.0      .98      3       34
  226.     western           WE      Sharon Gray           5.3     .97      5       23
  227.     southwest       SW      Lewis Dalsass         2.7      .8       2       18
  228.     southern         SO      Suan Chin               5.1      .95     4       15
  229.     southeast       SE       Patricia Hemenway    4.0      .7       4       17
  230.     eastern           EA      TB Savage               4.4      .84     5       20
  231.     northeast       NE       AM Main Jr.               5.1     .94      3       13
  232.     southeast       SE      Patricia Hemenway      4.0     .7       4       17
  233.     central            CT      Ann Stephens            5.7     .94     5       13
复制代码
来自:http://www.cnblogs.com/stephen-l ... /11/17/2245130.html

目录:
Linux Shell常用技巧(一)
http://www.acejoy.com/thread-4312-1-1.html
一. 特殊文件: /dev/null和/dev/tty
二. 简单的命令跟踪
三. 正则表达式基本语法描述
四. 使用cut命令选定字段
五. 计算行数、字数以及字符数
六. 提取开头或结尾数行

Linux Shell常用技巧(二)
http://www.acejoy.com/thread-4313-1-1.html
七. grep家族

Linux Shell常用技巧(三)
http://www.acejoy.com/thread-4314-1-1.html
八. 流编辑器sed

Linux Shell常用技巧(四)
http://www.acejoy.com/thread-4315-1-1.html
九. awk实用功能
十. awk表达式功能

Linux Shell常用技巧(五)
http://www.acejoy.com/thread-4316-1-1.html
十一.awk编程

Linux Shell常用技巧(六)
http://www.acejoy.com/thread-4317-1-1.html
十二.行的排序命令sort
十三.删除重复行的命令uniq
十四.文件压缩解压命令tar
十五.大文件拆分命令split

Linux Shell常用技巧(七)
http://www.acejoy.com/thread-4318-1-1.html
十六.文件查找命令find
十七.xargs命令

Linux Shell常用技巧(八)
http://www.acejoy.com/thread-4319-1-1.html
十八.和系统运行状况相关的Shell命令

Linux Shell常用技巧(九)
http://www.acejoy.com/thread-4320-1-1.html
十九.和系统运行进程相关的Shell命令

Linux Shell常用技巧(十)
http://www.acejoy.com/thread-4321-1-1.html
二十.通过管道组合Shell命令获取系统运行数据
二十一.通过管道组合Shell命令进行系统管理

Linux Shell常用技巧(十一)
http://www.acejoy.com/thread-4322-1-1.html
二十二.交互式使用Bash Shell

Linux Shell常用技巧(十二)
http://www.acejoy.com/thread-4323-1-1.html
二十三.Bash Shell编程

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-8 00:56 , Processed in 0.019985 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表