Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1145943
  • 博文数量: 646
  • 博客积分: 288
  • 博客等级: 二等列兵
  • 技术积分: 5375
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-08 14:33
个人简介

为了技术,我不会停下学习的脚步,我相信我还能走二十年。

文章分类

全部博文(646)

文章存档

2014年(8)

2013年(134)

2012年(504)

分类:

2012-07-06 18:22:01

原文地址:SED相关积累 作者:g_programming

目录               

 TOC \o "1-3" \h \z \u (一)文本间隔:. PAGEREF _Toc323292466 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400360036000000

1.          每行后增加一空行. PAGEREF _Toc323292467 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400360037000000

2.          每行后只有一空行。. PAGEREF _Toc323292468 \h 4 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400360038000000

3.          每行后增加两空行. PAGEREF _Toc323292469 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400360039000000

4.          删除偶数行. PAGEREF _Toc323292470 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370030000000

5.          匹配“regex”的行前插一空行. PAGEREF _Toc323292471 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370031000000

6.          匹配“regex”的行后插入一空行. PAGEREF _Toc323292472 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370032000000

7.          匹配“regex”行前后各插入一空行. PAGEREF _Toc323292473 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370033000000

(二)编号:. PAGEREF _Toc323292474 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370034000000

1.          文件编号,左对齐(制表符隔开). PAGEREF _Toc323292475 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370035000000

2.          文件编号(行号在左,文字右端对齐). PAGEREF _Toc323292476 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370036000000

3.          文件编号,只显示非空白行的行号. PAGEREF _Toc323292477 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370037000000

4.          计算行数 (模拟 "wc -l". PAGEREF _Toc323292478 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370038000000

(三)文本转换和替代:. PAGEREF _Toc323292479 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400370039000000

1.          Unix环境:转换DOS的换行符(CR/LF)为Unix的LF格式. PAGEREF _Toc323292480 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380030000000

2.          Unix环境:转换Unix的新行符(LF)为DOS格式. PAGEREF _Toc323292481 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380031000000

3.          DOS环境:转换Unix新行符(LF)为DOS格式. PAGEREF _Toc323292482 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380032000000

4.          DOS环境:转换DOS新行符(CR/LF)为Unix格式. PAGEREF _Toc323292483 \h 5 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380033000000

5.          去掉ascii码为032的字符. PAGEREF _Toc323292484 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380034000000

6.          前导的“空白字符”(空格,制表符)删除使之左对齐. PAGEREF _Toc323292485 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380035000000

7.          末尾的“空白字符”(空格,制表符)删除. PAGEREF _Toc323292486 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380036000000

8.          前导和拖尾的空白字符删除. PAGEREF _Toc323292487 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380037000000

9.          开头处插5个空格. PAGEREF _Toc323292488 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380038000000

10.         文本右对齐(以79个字符为宽度). PAGEREF _Toc323292489 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400380039000000

11.         文本居中(以79个字符为宽度). PAGEREF _Toc323292490 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390030000000

12.         “foo”替换为“bar”(相关). PAGEREF _Toc323292491 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390031000000

13.         有“baz”将“foo”替换成“bar”. PAGEREF _Toc323292492 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390032000000

14.         无“baz”将“foo”替换成“bar”. PAGEREF _Toc323292493 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390033000000

15.         “scarlet”“ruby”或“puce”换成“red”. PAGEREF _Toc323292494 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390034000000

16.         倒置所有行. PAGEREF _Toc323292495 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390035000000

17.         字符逆序. PAGEREF _Toc323292496 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390036000000

18.         每两行连接成一行(类似“paste”). PAGEREF _Toc323292497 \h 6 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390037000000

19.         以“”结束将下一行并到当前行末尾. PAGEREF _Toc323292498 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390038000000

20.         等号开头,将当前行并到上一行末尾. PAGEREF _Toc323292499 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003400390039000000

21.         为数字字串增加逗号分隔符号将“4567”改为“4,567”. PAGEREF _Toc323292500 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300030000000

22.         为带有小数点和负号的数值增加逗号分隔符. PAGEREF _Toc323292501 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300031000000

23.         每5行后增加一空白行. PAGEREF _Toc323292502 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300032000000

24.         将A…B…C替换成foo(B最接近的A,C. PAGEREF _Toc323292503 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300033000000

25.         执行重复的正则表达. PAGEREF _Toc323292504 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300034000000

26.         匹配AA追加YES,否则追加NO PAGEREF _Toc323292505 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300035000000

27.         合并相关多行. PAGEREF _Toc323292506 \h 7 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300036000000

28.         替换第4次出现的regex为AA PAGEREF _Toc323292507 \h 8 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300037000000

29.         去除字符串内重复的(从左到右). PAGEREF _Toc323292508 \h 8 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300038000000

30.         pattern行插入some,追加some,更改为some PAGEREF _Toc323292509 \h 8 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500300039000000

31.         将System Administrator(可能出现在不同行)替换Desktop Users PAGEREF _Toc323292510 \h 8 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310030000000

32.         将一行按照行首拆为多行. PAGEREF _Toc323292511 \h 8 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310031000000

(四)选择性地显示特定列:. PAGEREF _Toc323292512 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310032000000

1.          查看ps aux的命令结果的COMMAND. PAGEREF _Toc323292513 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310033000000

(五)选择性地显示特定行:. PAGEREF _Toc323292514 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310034000000

1.          显示文件中的前10. PAGEREF _Toc323292515 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310035000000

2.          显示文件中的第一行. PAGEREF _Toc323292516 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310036000000

3.          显示文件中的最后10. PAGEREF _Toc323292517 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310037000000

4.          显示文件中的最后2. PAGEREF _Toc323292518 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310038000000

5.          显示文件中的最后一行. PAGEREF _Toc323292519 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500310039000000

6.          显示文件中的倒数第二行. PAGEREF _Toc323292520 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320030000000

7.          只显示匹配正则表达式的行. PAGEREF _Toc323292521 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320031000000

8.          只显示“不”匹配正则表达式的行. PAGEREF _Toc323292522 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320032000000

9.          显示“regexp”匹配行的上一行. PAGEREF _Toc323292523 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320033000000

10.         显示“regexp”匹配行的下一行. PAGEREF _Toc323292524 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320034000000

11.         显示包含“regexp”的行及其前后行及第一行之前当前行号. PAGEREF _Toc323292525 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320035000000

12.         显示包含“AAA”、“BBB”或“CCC”的一行(任意次序). PAGEREF _Toc323292526 \h 9 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320036000000

13.         显示包含“AAA”、“BBB”和“CCC”的一行(固定次序). PAGEREF _Toc323292527 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320037000000

14.         显示包含“AAA”“BBB”或“CCC”的行. PAGEREF _Toc323292528 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320038000000

15.         显示包含“AAA”的段落. PAGEREF _Toc323292529 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500320039000000

16.         显示包含“AAA”“BBB”和“CCC”的段落 (任意次序). PAGEREF _Toc323292530 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330030000000

17.         显示包含“AAA”、“BBB”或“CCC”的段落 (任意次序). PAGEREF _Toc323292531 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330031000000

18.         显示包含65个或以上字符的行. PAGEREF _Toc323292532 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330032000000

19.         显示包含65个以下字符的行. PAGEREF _Toc323292533 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330033000000

20.         显示部分文本(范围显示) PAGEREF _Toc323292534 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330034000000

21.         显示部分文本(指定行号范围). PAGEREF _Toc323292535 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330035000000

22.         显示部分文本(指定行号范围,多文本). PAGEREF _Toc323292536 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330036000000

23.         显示第52. PAGEREF _Toc323292537 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330037000000

24.         第3行开始每7行显示一次. PAGEREF _Toc323292538 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330038000000

25.         显示两个正则表达式之间的文本(包含). PAGEREF _Toc323292539 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500330039000000

26.         显示两个正则表达式之间的文本(不包含). PAGEREF _Toc323292540 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340030000000

27.         显示两个正则表达式之间的文本(贪婪匹配). PAGEREF _Toc323292541 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340031000000

28.         匹配多行‘abc\nefg’. PAGEREF _Toc323292542 \h 10 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340032000000

(六)选择性地删除特定行:. PAGEREF _Toc323292543 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340033000000

1.          显示除了两个正则表达式之间的内容. PAGEREF _Toc323292544 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340034000000

2.         删除文件中相邻的重复行(模拟“uniq”). PAGEREF _Toc323292545 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340035000000

3.          删除文件中的重复行,不管有无相邻. PAGEREF _Toc323292546 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340036000000

4.          删除除重复行外的所有行(模拟“uniq -d”). PAGEREF _Toc323292547 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340037000000

5.          删除文件中开头的10. PAGEREF _Toc323292548 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340038000000

6.          删除文件中的最后一行. PAGEREF _Toc323292549 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500340039000000

7.          删除文件中的最后两行. PAGEREF _Toc323292550 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350030000000

8.          删除文件中的最后10. PAGEREF _Toc323292551 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350031000000

9.          删除8的倍数行. PAGEREF _Toc323292552 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350032000000

10.         删除匹配式样的行. PAGEREF _Toc323292553 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350033000000

11.         删除文件中的所有空行. PAGEREF _Toc323292554 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350034000000

12.         保留多个相邻空行的第一行并删除文件顶部和尾部的空行. PAGEREF _Toc323292555 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350035000000

13.         只保留多个相邻空行的前两行. PAGEREF _Toc323292556 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350036000000

14.         删除文件顶部的所有空行. PAGEREF _Toc323292557 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350037000000

15.         删除文件尾部的所有空行. PAGEREF _Toc323292558 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350038000000

16.         删除每个段落的最后一行. PAGEREF _Toc323292559 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500350039000000

17.         保留regex和它的下一行. PAGEREF _Toc323292560 \h 11 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360030000000

(七)特殊应用:. PAGEREF _Toc323292561 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360031000000

1.          移除手册页(man page)中的nroff标记. PAGEREF _Toc323292562 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360032000000

2.          提取新闻组或 e-mail 的邮件头. PAGEREF _Toc323292563 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360033000000

3.          提取新闻组或 e-mail 的正文部分. PAGEREF _Toc323292564 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360034000000

4.          从邮件头提取“Subject”(标题栏字段). PAGEREF _Toc323292565 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360035000000

5.          从邮件头获得回复地址. PAGEREF _Toc323292566 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360036000000

6.          获取邮件地址. PAGEREF _Toc323292567 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360037000000

7.          在每行开头加个尖括号和空格. PAGEREF _Toc323292568 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360038000000

8.          将每行开头处的尖括号和空格删除. PAGEREF _Toc323292569 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500360039000000

9.          移除大部分的HTML标签(包括跨行标签). PAGEREF _Toc323292570 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370030000000

10.         将分成多卷的uuencode文件解码. PAGEREF _Toc323292571 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370031000000

11.         将文件中的段落以字母顺序排序. PAGEREF _Toc323292572 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370032000000

12.         分别压缩每个.TXT文件. PAGEREF _Toc323292573 \h 12 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370033000000

(八)目录相关:. PAGEREF _Toc323292574 \h 13 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370034000000

1.          获取文件名tt.txt (/root/home/tt.txt) PAGEREF _Toc323292575 \h 13 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370035000000

2.          获取文件夹名/root/home (/root/home/tt.txt) PAGEREF _Toc323292576 \h 13 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003300320033003200390032003500370036000000

 

(一)文本间隔:


 sed G


 sed '/^$/d;G'
#d导致读入新行并且脚本回到起点


 sed 'G;G'


 sed 'n;d'
#n导致读入新行并且输出模式空间的内容,但是不回到脚本起点


 sed '/regex/{x;p;x;}'

6.       
 sed '/regex/G'


 sed '/regex/{x;p;x;G;}'

(二)编号:


 sed = filename | sed 'N;s/\n/\t/'


 sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'


 sed '/./=' filename | sed '/./N; s/\n/ /'


 sed -n '$='

(三)文本转换和替代:

环境:转换DOS的换行符(CR/LF)为UnixLF格式
 sed 's/.$//'                     #
假设所有行以CR/LF结束
 sed 's/^M$//'                    #
bash/tcsh中,将按Ctrl-M改为按Ctrl-V
 
sed 's/\x0D$//'                  # ssedgsed 3.02.80,及更高版本

sed -i 's/\r$//'  filename         # 文件在windows下打开会自动在每行之后加上\r=x0d

环境:转换Unix的新行符(LF)为DOS格式
 sed "s/$/`echo -e \\\r`/"        #
ksh下所使用的命令
 sed 's/$'"/`echo \\\r`/"         #
bash下所使用的命令
 sed "s/$/`echo \\\r`/"           #
zsh下所使用的命令
 sed 's/$/\r/'                    # gsed 3.02.80
及更高版本

3.       环境:转换Unix新行符(LF)为DOS格式
 sed "s/$//"                      #
方法 1
 sed -n p                         #
方法 2

环境:转换DOS新行符(CR/LF)为Unix格式

下面的脚本只对UnxUtils sed 4.0.7 及更高版本有效。要识别UnxUtils版本的sed可以通过其特有的“--text”选项。你可以使用帮助选项(“--help”)看其中有无一个“--text”项以此来判断所使用的是否是UnxUtils版本。其它DOS版本的的sed则无法进行这一转换。但可以用“tr”来实现这一转换。
 sed "s/\r//" infile >outfile     # UnxUtils sed v4.0.7
或更高版本
 tr -d \r outfile        # GNU tr 1.22
或更高版本

sed -i 's/\o032//g'


 sed 's/^[ \t]*//'                #
见本文末尾关于'\t'用法的描述


 sed 's/[ \t]*$//'                #
见本文末尾关于'\t'用法的描述


 sed 's/^[ \t]*//;s/[ \t]*$//'


 sed 's/^/     /'


 sed -e :a -e 's/^.\{1,78\}$/ &/;t a'  # 78
个字符外加最后的一个空格

在方法1中,为了让文本居中每一行的前头和后头都填充了空格。 在方法2中,在居中文本的过程中只在文本的前面填充空格,并且最终这些空格将有一半会被删除。此外每一行的后头并未填充空格。
 sed  -e :a -e 's/^.\{1,77\}$/ & /;t a'                     #
方法1
 sed  -e :a -e 's/^.\{1,77\}$/ &/;t a' -e 's/\( *\)\1/\1/'  #
方法2


 sed 's/foo/bar/'                 # 只替换每一行中的第一个“foo”字串
 sed 's/foo/bar/4'                #
只替换每一行中的第四个“foo”字串
 sed 's/foo/bar/g'                #
将每一行中的所有“foo”都换成“bar
 sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' #
替换倒数第二个“foo
 sed 's/\(.*\)foo/\1bar/'            #
替换最后一个“foo


 sed '/baz/s/foo/bar/g'


 sed '/baz/!s/foo/bar/g'

15.    
 sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'  #对多数的sed都有效
 gsed 's/scarlet\|ruby\|puce/red/g'               #
只对GNU sed有效

gsed -r 's/scarlet|ruby|puce/red/g'               # 只对GNU sed有效

第一行成为最后一行,依次类推(模拟“tac”)。由于某些原因,使用下面命令时HHsed v1.5会将文件中的空行删除
 sed '1!G;h;$!d'               #
方法1
 sed -n '1!G;h;$p'             #
方法2

将行中的字符逆序排列,第一个字成为最后一字,……(模拟“rev”)
 sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'


 sed '$!N;s/\n/ /'

并去掉原来行尾的反斜杠和\n
 sed -e :a -e '/\\$/N; s/\\\n//; t a'

并以单个空格代替原来行头的“=
 sed -e :a -e '$!N;s/\n=/ /;t a' -e 'P;D'


 
gsed ':a;s/\B[0-9]\{3\}\>/,&/;t a'                     # GNU sed
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;t a'  # 其他sed

GNU sed
 gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;t a'

(在第5101520,等行后增加一空白行)
 gsed '0~5G'                      #
只对GNU sed有效
 sed 'n;n;n;n;G;'                 # 其他sed

B中中间,行AB前,行CB后直接的内容替换成foo

sed ':a;N;/\nA/b;/\nC/{s/.*\nB.*\n/FOO\n/;b};b a' #不包括行A和行C

sed ':a;N;/\nA/b;/\nC/{s/.*\nB.*\nC/FOO\n/;b};b a' #不包括行A

sed ‘/A/!b;a:N; /\nC$/!{ba};/\nC$/{s/A[^A]*\nB.*\nC$/FOO\n/;} #包括行A和行C

sed ‘s/foo/bar/3;s//que/’#将第三个foor替换为bar,第一个foo替换为que

sed '/^AA/s/$/ YES/;t;s/$/ NO/' urfile

sed '/^AA/ba;s/$/ NO/;b;:a;s/$/ YES/' urfile

ly5066113@ubuntu:~$ cat urfile

114.113.144.2:

19ms

19ms

218.61.204.73:

0ms

0ms

0ms

ly5066113@ubuntu:~$ sed ':a;$!N;/ms$/s/\n/ /;ta;P;D' urfile

114.113.144.2: 19ms 19ms

218.61.204.73: 0ms 0ms 0ms 0ms

 

sed '/regex/{x;s/^/./;/^.\{4\}$/{x;s/regex/AA/;b};x}' urfile

字符串 'aaabcccaaabbbccc',连续的字母作为一个子字符串,从左往右,去重复后,变为 'aaabcccbbb'

sed -nr 's/([^\n])\1*/&\n/g;:a;s/(^|\n)([^\n]+)(\n(.*\n)*)\2\n/\1\2\3/;ta;s/\n//gp'

行插入some,追加some,更改为some

插入:s/^pattern/some&/

追加:s/pattern$/&some/

更改:s/pattern/some/

sed -r '/System/{N;s/System([\n ]+)Administrator/Desktop\1User/g;P;D;}' tt1.txt

原文:

1.      69  2  3  5  0

2.      70  2  3  6  0

3.      71  2  3  6  0

我想根据第一列为基础,后面有四列就输出四行,比如输出结果为

1.      69  2  

2.      69  3

3.      69  5

4.      69  0

5.      70  2

6.      70  3

7.      70  6

8.      70  0

9.      71  2

10.   71  3

11.   71  6

12.   71  0

33.    

 

 

 

(四)选择性地显示特定列:

sed -r 's/(\S+ *){10}//'

 

(五)选择性地显示特定行:

(模拟“head”的行为)
 sed 10q

(模拟“head -1”命令)
 sed q

(模拟“tail”)
 
sed -e :a -e '$q;N;11,$D;b a'

(模拟“tail -2”命令)
 sed '$!N;$!D'

(模拟“tail -1”)
 sed '$!d'                        #
方法1
 sed -n '$p'                      #
方法2


 sed -e '$!{h;d;}' -e x              #
当文件中只有一行时,输入空行
 sed -e '1{$q;}' -e '$!{h;d;}' -e x  #
当文件中只有一行时,显示该行
 sed -e '1{$d;}' -e '$!{h;d;}' -e x  #
当文件中只有一行时,不输出

(模拟“grep”)
 sed -n '/regexp/p'               #
方法1
 sed '/regexp/!d'                 #
方法2

(模拟“grep -v”)
 sed -n '/regexp/!p'              #
方法1,与前面的命令相对应
 sed '/regexp/d'                  #
方法2,类似的语法

,但并不显示匹配行
 sed -n '/regexp/{g;1!p;};h'

,但并不显示匹配行
 sed -n '/regexp/{n;p;}'

,加上“regexp”所在行的行号 (类似“grep -A1 -B1”)
 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h


 sed '/AAA/!d; /BBB/!d; /CCC/!d'  #
字串的次序不影响结果


 sed '/AAA.*BBB.*CCC/!d'

(模拟“egrep”)
 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    #
多数sed
 gsed '/AAA\|BBB\|CCC/!d'                        #
GNU sed有效

(段落间以空行分隔)HHsed v1.5 必须在“x;”后加入“G;”,接下来的3个脚本都是这样
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'


 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'


 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'         #
只对GNU sed有效


 sed -n '/^.\{65\}/p'


 sed -n '/^.\{65\}/!p'            #
方法1,与上面的脚本相对应
 sed '/^.\{65\}/d'                #
方法2,更简便一点的方法

——从包含正则表达式的行开始到最后一行结束(范围显示)
 sed -n '/regexp/,$p'

(从第8至第12行,含812行)
 sed -n '8,12p'                   #
方法1
 sed '8,12!d'                     #
方法2

(从第8至第12行,含812行)

sed -n -e 8,12p -s files


 sed -n '52p'                     #
方法1
 sed '52!d'                       #
方法2
 sed '52q;d'                      #
方法3, 处理大文件时更有效率

    
 gsed -n '3~7p'                   #
只对GNU sed有效
 sed -n '3,${p;n;n;n;n;n;n;}'     #
其他sed


 sed -n '/Iowa/,/Montana/p'       #
区分大小写方式

sed -n '/Iowa/{:a n;/Montana/d;p;ba}'       # 区分大小写方式

sed -n '/Iowa/{p;:a;n;H;/Montana/{x;s/\n//p;z;h};ba}' file

#z清空保持空间

sed -n '/abc$/{N;/abc\nefg/p;D;}'

29.    

 

(六)选择性地删除特定行:


 sed '/Iowa/,/Montana/d'

只保留重复行中的第一行,其他行删除
 sed '$!N; /^\(.*\)\n\1$/!P; D'

,注意hold space所能支持的缓存大小,或者使用GNU sed
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'


 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'


 sed '1,10d'


 sed '$d'


 sed 'N;$!P;$!D;$d'


 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   #
方法1
 sed -n -e :a -e '1,10!{P;N;D;};N;b a'  #
方法2


 gsed '0~8d'                           #
只对GNU sed有效
 sed 'n;n;n;n;n;n;n;d;'                #
其他sed


 sed '/pattern/d'                      #
删除含pattern的行。当然pattern
                                       #
可以换成任何有效的正则表达式

(与“grep '.' ”效果相同)
 sed '/^$/d'                           #
方法1
 sed '/./!d'                           #
方法2

非贪婪(模拟“cat -s”)
 sed '/./,/^$/!d'        #
方法1,删除文件顶部的空行,允许尾部保留一空行
 sed '/^$/N;/\n$/D'      #
方法2,允许顶部保留一空行,尾部不留空行


 sed '/^$/N;/\n$/N;//D'


 sed '/./,$!d'


 sed -e :a -e '/^\n*$/{$d;N;b a' -e '}'  #
对所有sed有效
 sed -e :a -e '/^\n*$/N;/\n$/b a'        #
同上,但只对 gsed 3.02.*有效


 sed -n '/^$/{p;h;};/./{x;/./p;}'

sed -n '/regex/{N;p}'

 

(七)特殊应用:

。在Unix System Vbash shell下使用'echo'命令时可能需要加上 -e 选项。
 sed "s/.`echo \\\b`//g"    #
外层的双括号是必须的(Unix环境)
 sed 's/.^H//g'             #
bashtcsh, Ctrl-V 再按 Ctrl-H
 sed 's/.\x08//g'           # sed 1.5
GNU sedssed所使用的十六进制的表示方法


 sed '/^$/q'                # 删除第一行空行后的所有内容


 sed '1,/^$/d'              #
删除第一行空行之前的所有内容

,并移除开头的“Subject:”字样
 sed '/^Subject: */!d; s///;q'


 sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

。在上一个脚本所产生的那一行邮件头的基础上进一步的将非电邮地址的部分剃除。(见上一脚本)
 sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'

(引用信息)
 sed 's/^/> /'

(解除引用)
 sed 's/^> //'


 sed -e :a -e 's/<[^>]*>//g;/

。移除文件头信息,只保留uuencode编码部分。文件必须以特定顺序传给sed。下面第一种版本的脚本可以直接在命令行下输入;第二种版本则可以放入一个带执行权限的shell脚本中。(由Rahul Dhesi的一个脚本修改而来。)
 sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
 sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2

。段落间以(一行或多行)空行分隔。GNU sed使用字元“\v”来表示垂直制表符,这里用它来作为换行符的占位符——当然你也可以用其他未在文件中使用的字符来代替它。
 sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
 gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'

,压缩后删除原来的文件并将压缩后的.ZIP文件命名为与原来相同的名字(只是扩展名不同)。(DOS环境:“dir /b”显示不带路径的文件名)。
 echo @echo off >zipup.bat
 dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat

 

 

sed -n 's/\(.*\)\///p'

sed -n 's/\(.*\)\/.*/\1/p'

 

 

阅读(306) | 评论(0) | 转发(0) |
0

上一篇:AWK相关积累

下一篇:Linux命令相关积累

给主人留下些什么吧!~~