下面这些命令未必经常会用到,但当需要时,知道这些肯定是件好事。

一、多行命令

sed命令通常是对一行数据进行处理,然后下一行重复处理。

sed编辑器包含了三个可用来处理多行文本的特殊命令

  • N:将数据流中的下一行加进来创建一个多行组来处理
  • D:删除多行组中的一行
  • P:打印多行组中的一行

1.1 next命令

两种删除匹配的下一行的办法:

  1. cat data1.txt
  2. This is the header line.
  3.  
  4. This is a data line.
  5.  
  6. This is the last line.
  7.  
  8. sed '/^$/d' data1.txt
  9. This is the header line.
  10. This is a data line.
  11. This is the last line.
  12.  
  13. sed '/header/{n ; d}' data1.txt
  14. This is the header line.
  15. This is a data line.
  16.  
  17. This is the last line.

空行处理

1.2 合并文本行

用大写N,可以一次性读取两行。区别与n是移动到下一行。

  1. $ sed '/first/{ N ; s/\n/ / }' data2.txt
  2. This is the header line.
  3. This is the first data line. This is the second data line.
  4. This is the last line.
  5.  
  6. $ cat data2.txt
  7. This is the header line.
  8. This is the first data line.
  9. This is the second data line.
  10. This is the last line.
  11.  
  12. $ cat data3.txt
  13. On Tuesday, the Linux System
  14. Administrator's group meeting will be held.
  15. All System Administrators should attend.
  16. Thank you for your attendance.
  17.  
  18. $ sed 'N ; s/System.Administrator/Desktop User/' data3.txt
  19. On Tuesday, the Linux Desktop User's group meeting will be held.
  20. All Desktop Users should attend.
  21. Thank you for your attendance.

N使用例子

在N后面最好匹配多行命令,而单行命令则可以放在N前面,如:

  1. $ sed 'N
  2. > s/System\nAdministrator/Desktop\nUser/
  3. > s/System Administrator/Desktop User/
  4. > ' data4.txt
  5. On Tuesday, the Linux Desktop
  6. User's group meeting will be held.
  7. All System Administrators should attend.
  8.  
  9. $ cat data4.txt
  10. On Tuesday, the Linux System
  11. Administrator's group meeting will be held.
  12. All System Administrators should attend.
  13.  
  14. $ sed '
  15. > s/System Administrator/Desktop User/
  16. > N
  17. > s/System\nAdministrator/Desktop\nUser/
  18. > ' data4.txt
  19. On Tuesday, the Linux Desktop
  20. User's group meeting will be held.
  21. All Desktop Users should attend.

前后写的例子

1.3 删除多行命令

如果在N后面用d,就会把多行都一起删除。但是如果用D,就会删除到\n为止。

  1. $ sed 'N ; /System\nAdministrator/d' data4.txt
  2. All System Administrators should attend.
  3. $ cat data4.txt
  4. On Tuesday, the Linux System
  5. Administrator's group meeting will be held.
  6. All System Administrators should attend.
  7. $ sed 'N ; /System\nAdministrator/D' data4.txt
  8. Administrator's group meeting will be held.
  9. All System Administrators should attend.

d和D的对比

然后有删除header前的空白行

  1. $ sed '/^$/{N ; /header/D}' data5.txt
  2. This is the header line.
  3. This is a date line.
  4.  
  5. This is the last line.
  6.  
  7. $ sed '{N ; /header/D}' data5.txt
  8. This is a date line.
  9.  
  10. This is the last line.
  11. $ sed '{N ; /header/d}' data5.txt
  12. This is a date line.
  13.  
  14. This is the last line.
  15. $ cat data5.txt
  16.  
  17. This is the header line.
  18. This is a date line.
  19.  
  20. This is the last line.

删除header前的空行

类似的和D相似的,P也是会和N配合输出第一行

  1. $ cat data3.txt
  2. On Tuesday, the Linux System
  3. Administrator's group meeting will be held.
  4. All System Administrators should attend.
  5. Thank you for your attendance.
  6. $ sed -n 'N ; /System\nAdministrator/P' data3.txt
  7. On Tuesday, the Linux System

P的使用

二、保持空间

sed编辑器有一块称作保持空间的缓冲区域。在处理某些行时,可以用保持空间来临时保存一些行。

  • h:将模式空间那关键复制到保持空间
  • H:将模式空间附加到保持空间
  • g:将保持空间复制到模式空间
  • G:将保持空间附加到模式空间
  • x:交换模式空间和保持空间的内容
  1. $ cat data2.txt
  2. This is the header line.
  3. This is the first data line.
  4. This is the second data line.
  5. This is the last line.
  6. $ sed -n '/first/ {h ; p; n ; p ; g ; p }' data2.txt
  7. This is the first data line.
  8. This is the second data line.
  9. This is the first data line.
  10.  
  11. $ sed -n '/first/ {h ; n ; p ; g ; p }' data2.txt
  12. This is the second data line.
  13. This is the first data line.

保持空间

三、排除命令

感叹号命令(!)用来排除命令

  1. $ sed -n '/header/!p' data2.txt
  2. This is the first data line.
  3. This is the second data line.
  4. This is the last line.
  5. $ sed 'N;
  6. > s/System\nAdministrator/Desktop\nUser/
  7. > s/System Administrator/Desktop User/
  8. > ' data4.txt
  9. On Tuesday, the Linux Desktop
  10. User's group meeting will be held.
  11. All System Administrators should attend.
  12. $ sed '$!N;
  13. > s/System\nAdministrator/Desktop\nUser/
  14. > s/System Administrator/Desktop User/
  15. > ' data4.txt
  16. On Tuesday, the Linux Desktop
  17. User's group meeting will be held.
  18. All Desktop Users should attend.

!例子

然后是将文件反转的例子:

  1. $ cat data2.txt
  2. This is the header line.
  3. This is the first data line.
  4. This is the second data line.
  5. This is the last line.
  6. $ sed -n '{1!G ; h ; $p }' data2.txt
  7. This is the last line.
  8. This is the second data line.
  9. This is the first data line.
  10. This is the header line.

反转例子

四、改变流

4.1 分支指令

基于地址、地址模式或地址区间排除一整块命令,这允许你只对数据流中的特定行执行一组命令。

分支命令b格式:[address]b [label]

address参数:决定了那些行的数据会触发分支明林。

label参数:定义了要跳转到的位置。

  1. $ cat data2.txt
  2. This is the header line.
  3. This is the first data line.
  4. This is the second data line.
  5. This is the last line.
  6.  
  7. $ sed '{2,3b ; s/This is/Is this/ ; s/line./test?/}' data2.txt
  8. Is this the header test?
  9. This is the first data line.
  10. This is the second data line.
  11. Is this the last test?

命令b使用例子

上面的例子里,分支跳过了2,3两行。如果不想直接跳转到脚本结尾,可为分支命令定义一个要跳转到的标签。

标签最多长7个字符,例子:

  1. chen@ubuntu:~/shell/ch21$ sed '{/first/b jump1 ; s/This is the/No jump on/
  2. > :jump1
  3. > s/This is the/Jump here on/}' data2.txt
  4. No jump on header line.
  5. Jump here on first data line.
  6. No jump on second data line.
  7. No jump on last line.
  8.  
  9. chen@ubuntu:~/shell/ch21$ cat data2.txt
  10. This is the header line.
  11. This is the first data line.
  12. This is the second data line.
  13. This is the last line.

命令b跳转例子

如果文中出现first,程序就跳转到jump1脚本行。如果没有匹配,sed则会继续执行脚本中的命令,包括分支标签后的命令。

  1. chen@ubuntu:~/shell/ch21$ echo "This, is, a, test, to, remove, commas." | sed -n '{
  2. > :start
  3. > s/,//1p
  4. > b start
  5. > }'
  6. This is, a, test, to, remove, commas.
  7. This is a, test, to, remove, commas.
  8. This is a test, to, remove, commas.
  9. This is a test to, remove, commas.
  10. This is a test to remove, commas.
  11. This is a test to remove commas.
  12. ^C

命令b用作循环

要能停止循环,只要加上模式匹配在命令b前面就好了

  1. chen@ubuntu:~/shell/ch21$ echo "This, is, a, test, to, remove, commas." | sed -n '{
  2. > :start
  3. > s/,//1p
  4. > /,/b start
  5. > }'
  6. This is, a, test, to, remove, commas.
  7. This is a, test, to, remove, commas.
  8. This is a test, to, remove, commas.
  9. This is a test to, remove, commas.
  10. This is a test to remove, commas.
  11. This is a test to remove commas.

改进例子

4.2 测试指令

类似于分支命令,测试test命令(t)也可以用来改变sed编辑器脚本执行流程。

如果天幻命令成功匹配并替换了一个模式,测试命令就会跳转到指定标签。如果替换命令未能匹配指定模式,测试命令就不会跳转。

测试命令格式与分支命令格式相同:[address]t [label]

和分支命令一样,如果没有标签的情况下,如果测试成功,sed会跳转到脚本结尾。

测试命令基本上就是一个if-then

  1. chen@ubuntu:~/shell/ch21$ sed '{
  2. > s/first/matched/
  3. > t
  4. > s/This is the/No match on/
  5. > }' data2.txt
  6. No match on header line.
  7. This is the matched data line.
  8. No match on second data line.
  9. No match on last line.
  10. chen@ubuntu:~/shell/ch21$ cat data2.txt
  11. This is the header line.
  12. This is the first data line.
  13. This is the second data line.
  14. This is the last line.

测试命令例子

测试命令的循环方法:

  1. chen@ubuntu:~/shell/ch21$ echo "This, is, a, test, to, remove, commas." | sed -n '{
  2. > :start
  3. > s/,//1p
  4. > t start
  5. > }'
  6. This is, a, test, to, remove, commas.
  7. This is a, test, to, remove, commas.
  8. This is a test, to, remove, commas.
  9. This is a test to, remove, commas.
  10. This is a test to remove, commas.
  11. This is a test to remove commas.

测试命令循环方法

五、模式替代

在使用通配符时,很难知道到底哪些文本会匹配模式。

  1. chen@ubuntu:~/shell/ch21$ echo "The cat sleeps in his hat." | sed 's/cat/"cat"/'The "cat" sleeps in his hat.
  2.  
  3. chen@ubuntu:~/shell/ch21$ echo "The cat sleeps in his hat." | sed 's/.at/".at"/g'
  4. The ".at" sleeps in his ".at".

通配符无法再引号中使用

5.1 &符号

用&符号可以用来代表替换命令中的匹配的模式。不管匹配出来什么样的文本,都可以使用&符号,来使用这段文本。

  1. myfly2@ubuntu:~/shell/ch21$ echo "The cat sleeps in his hat." | sed 's/.at/"&"/g'
  2. The "cat" sleeps in his "hat".

&符号例子

5.2 替代单独的单词

有时候我们不需要整个字符串,只想提取字符串的一部分。

sed编辑器用圆括号来定义替换模式中的子模式,然后用特殊字符来引用每个子模式。

  1. chen@ubuntu:~/shell/ch21$ echo "The System Administrator manual" | sed '
  2. > s/\(System\) Administrator/\ User/'
  3. The System User manual
  4.  
  5. chen@ubuntu:~/shell/ch21$ echo "The System Administrator manual" | sed '
  6. s/System \(Administrator\)/\ User/'
  7. The Administrator User manual
  8.  
  9. chen@ubuntu:~/shell/ch21$ echo "That furry cat is pretty" | sed 's/furry \(.at\)/\1/'
  10. That cat is pretty
  11. chen@ubuntu:~/shell/ch21$ echo "That furry hat is pretty" | sed 's/furry \(.at\)/\1/'
  12. That hat is pretty
  13. #当需要在两个或多个子模式间插入文本时,这个特性尤其有用
  14. chen@ubuntu:~/shell/ch21$ echo "" | sed '{
  15. > :start
  16. > s/\(.*[-]\)\([-]\{\}\)/\,\/
  17. > t start
  18. > }'
  19. ,,
  20. #分成两部分:
  21. #.*[-]
  22. #[-]{}
  23. #第一个子模式是以数字结尾的任意长度的字符。
  24. #第二个子模式是若干组三位数字

子模式例子

六、在脚本中使用sed

使用包装脚本,sed脚本过程繁琐,如果脚本很长的话。可以将sed编辑器命令放到shell包装脚本中。

这样可以不用每次都键入脚本。

  1. chen@ubuntu:~/shell/ch21$ cat reverse.sh
  2. #!/bin/bash
  3. # Shell wrapper for sed editor script.
  4. # to reverse text file lines.
  5. #
  6. sed -n '{ 1!G ; h ; $p }' $
  7. #
  8. chen@ubuntu:~/shell/ch21$ chmod +x reverse.sh
  9. chen@ubuntu:~/shell/ch21$ ls
  10. data2.txt data4.txt reverse.sh
  11. chen@ubuntu:~/shell/ch21$ ./reverse.sh data2.txt
  12. This is the last line.
  13. This is the second data line.
  14. This is the first data line.
  15. This is the header line.

写成文件形式的sed

6.2 重定向sed的输出

  1. chen@ubuntu:~/shell/ch21$ cat fact.sh
  2. #!/bin/bash
  3. # Add commas to number in factorial answer
  4. #
  5. factorial=
  6. counter=
  7. number=$
  8. #
  9. while [ $counter -le $number ]
  10. do
  11. factorial=$[ $factorial * $counter ]
  12. counter=$[ $counter + ]
  13. done
  14. #
  15. result=$(echo $factorial | sed '{
  16. :start
  17. s/\(.*[-]\)\([-]\{\}\)/\,\/
  18. t start
  19. }')
  20. #
  21. echo "The result is $result"
  22. #
  23. chen@ubuntu:~/shell/ch21$ ./fact.sh
  24. The result is ,,,,,,

可以发sed结果保存

七、创建sed实用工具

7.1 加倍行间距

  1. chen@ubuntu:~/shell/ch21$ sed 'G' data2.txt
  2. This is the header line.
  3.  
  4. This is the first data line.
  5.  
  6. This is the second data line.
  7.  
  8. This is the last line.
  9.  
  10. chen@ubuntu:~/shell/ch21$ sed '$!G' data2.txt
  11. This is the header line.
  12.  
  13. This is the first data line.
  14.  
  15. This is the second data line.
  16.  
  17. This is the last line.

加倍行间距

7.2 对可能还有空白行的文件加倍行间距

  1. #先删除原来空行,然后再加空行
  2. chen@ubuntu:~/shell/ch21$ sed '/^$/d;$!G' data6.txt
  3. This is line one.
  4.  
  5. This is line two.
  6.  
  7. This is line three.
  8.  
  9. This is line four.
  10. chen@ubuntu:~/shell/ch21$

加倍行间距例子

7.3 给文件中的行编行

  1. chen@ubuntu:~/shell/ch21$ sed '=' data2.txt
  2.  
  3. This is the header line.
  4.  
  5. This is the first data line.
  6.  
  7. This is the second data line.
  8.  
  9. This is the last line.
  10. chen@ubuntu:~/shell/ch21$ sed '=' data2.txt | sed 'N; s/\n/ /'
  11. This is the header line.
  12. This is the first data line.
  13. This is the second data line.
  14. This is the last line.
  15. chen@ubuntu:~/shell/ch21$
  16.  
  17. #=号命令增加行号
  18. #N命令将两行合并成一行
  19. #然后把换行符换成空格

增加行号的脚本

7.4 打印末尾行

  1. chen@ubuntu:~/shell/ch21$ sed '{
  2. :start
  3. $q ; N ; ,$D
  4. b start
  5. }' data7.txt
  6. This is line .
  7. This is line .
  8. This is line .
  9. This is line .
  10. This is line .
  11. This is line .
  12. This is line .
  13. This is line .
  14. This is line .
  15. This is line .
  16. chen@ubuntu:~/shell/ch21$ cat data7.txt
  17. This is line .
  18. This is line .
  19. This is line .
  20. This is line .
  21. This is line .
  22. This is line .
  23. This is line .
  24. This is line .
  25. This is line .
  26. This is line .
  27. This is line .
  28. This is line .
  29. This is line .
  30. This is line .
  31. This is line .

打印行尾

7.5 删除行

删除不需要的空白行,如果有选择的删除空白行,需要一点创造力。

7.5.1 删除连续的空白行

  1. chen@ubuntu:~/shell/ch21$ sed '/./,/^$/!d' data8.txt
  2. This is line one.
  3.  
  4. This is line two.
  5.  
  6. This is line three.
  7.  
  8. This is line four.
  9. chen@ubuntu:~/shell/ch21$ cat data8.txt
  10. This is line one.
  11.  
  12. This is line two.
  13.  
  14. This is line three.
  15.  
  16. This is line four.

删除连续的空白行例子

7.5.2 删除开头的空白行

  1. chen@ubuntu:~/shell/ch21$ cat data9.txt
  2.  
  3. This is line one.
  4.  
  5. This is line two.
  6. chen@ubuntu:~/shell/ch21$ sed '/./,$!d' data9.txt
  7. This is line one.
  8.  
  9. This is line two.

脚本例子

7.5.3 删除结尾的空白行

  1. chen@ubuntu:~/shell/ch21$ sed '{
  2. > :start
  3. > /^\n*$/{$d ; N ; b start}
  4. > }' data10.txt
  5. This is the first line.
  6. This is the seconde line.
  7. chen@ubuntu:~/shell/ch21$ cat data10.txt
  8. This is the first line.
  9. This is the seconde line.

例子

7.6 删除HTML标签

sed进阶的更多相关文章

  1. 第11章:sed进阶操作

    第11章:sed进阶操作 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法 sed命令行格式为 ...

  2. 《Linux命令行与shell脚本编程大全》第二十一章 sed进阶

    本章介绍一些sed编辑器提供的高级特性. 21.1 多行命令 按照之前的知识,所有的sed编辑器命令都是针对单行数据执行操作的. 在sed编辑器读取数据流时,它会基于换行符的位置将数据分成行,一次处理 ...

  3. sed进阶N;P;D

    案例 sed 的高级替换 $cat file1 why:1 why:2 3 4 5 why:6 why:7 8 why:9 $cat file2 why:1 why:2 3 4 5 why:6 why ...

  4. sed进阶教程

    寻址规则 常规寻址 如果没有指定地址,那么命令将应用于每一行. 如果只有一个地址,那么命令应用于与这个地址匹配的任意行. 如果指定了由逗号分隔的两个地址,那么命令应用于匹配第一个地址(不包括第一个地址 ...

  5. 文本处理工具——sed进阶

    一sed的搜索替代 (一)常见的和替代相关的选项 搜索替代,和vim的写法很像 s///:查找替换,支持使用其它分隔符,s@@@,s### p: 显示替换成功的行,就是打印. w /PATH/TO/S ...

  6. Shell编程—sed进阶

    1多行命令 sed编辑器包含了三个可用来处理多行文本的特殊命令. N:将数据流中的下一行加进来创建一个多行组来处理. D:删除多行组中的一行. P:打印多行组中的一行. 1.1next命令 1. 单行 ...

  7. sed命令总结-Linux

    sed命令总结-Linux linuxsed 2018年02月08日 19时27分57秒 命令语法经常忘记,每次总是看笔记不切实际,记不起来的要多查manual,本次总结按照manual总结,希望下次 ...

  8. 两个有用的shell工具总结

    shell工具之一:sed sed基础 sed编辑器被称作流编辑器,与常见的交互式文本编辑器刚好相反.文本编辑器可以通过键盘来交互式地插入.删除.替换文本中的数据:而流编辑器是基于一组预先的规则来编辑 ...

  9. 《Linux命令行与shell脚本编程大全 第3版》

    第一部分 Linux 命令行 第1章  初识Linux she1.1   什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...

随机推荐

  1. Vue中解决路由切换,页面不更新的实用方法

    前言:vue-router的切换不同于传统的页面的切换.路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新,也就是我们口中的页 ...

  2. JsonDatetime

    ToDatetime public DateTime JsonDateTimeConvert(string time) { //try //{ if (String.IsNullOrEmpty(tim ...

  3. Python解决ModuleNotFoundError: No module named 'Queue'的问题

    我们知道Python2和Python3两个版本之间,有些不兼容的地方,Python3中引入Queue会报出这个问题. Python3中要这样引入: import queue Python2中要这样引入 ...

  4. 【ABAP系列】SAP ABAP 生成随机数的函数

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 生成随机数的函数 ...

  5. 剑指offer--day02

    1.1题目:用两个栈实现队列:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 1.2解题思路: 创建两个栈stack1和stack2,使用两个“先进后出”的栈实现 ...

  6. 设置IIS的gzip

    如果服务器iis 中没有配置动态压缩的话,在性能中选项中配置. 设置成功之后:

  7. STL 配接器(adapters)

    定义 配接器(adapters):将一个class的接口,转换为另一个class的接口,使得原来不能一起使用相互兼容的classes,可以一起协同工作. 配接器是一种设计模式. STL中提供的各种配接 ...

  8. CDH平台搭建解决离线安装依赖包的方法

    背景介绍: 1CDH开发平台在搭建的过程中,会遇到各种各样的问题,其中的各种依赖就是一个很让人头痛的问题.如果安装脚本文件出现了这种问题,那么就可以把以下的这种方法加入shell中,但是不要用yum来 ...

  9. HTML5随记

    1.浏览器加载HTML的过程是从上至下,因此引用的第三方js文件一定要放到自己定义的js文件的前面,否则引入的js文件将会在加载时失效. 2.html的全局属性包括:accesskey.content ...

  10. jmeter测试结果jtl字段分析

    1  Bytes Throughput Over Time  每秒传输字节吞吐量,表明Jmeter在测试时,随着时间推移发送和接受的字节数 2  Response Codes per Second  ...