个人学习笔记--sed命令(进阶篇)
2016-08-02 13:36
381 查看
删除所有空行 [qs@qs ~]$ cat data1 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ sed '/^$/d' data1 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ 删除特定空行 [qs@qs ~]$ sed '/header/{n;d}' data1 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ #n命令将会是得sed编辑器移动到模式匹配到的行的下一行。(“下一行”的数据将进入占据模式空间) ------- [qs@qs ~]$ cat data2 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ sed '/first/{N;s/\n/ /}' data2 #合并行,将模式匹配到的行的下一行合并到模式匹配到的行。(“合并后的行”将占据模式空间) This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ [qs@qs ~]$ sed '/first/{N}' data2 #虽然看起来是两行,但是sed编辑器会将它们看成是一行。 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ sed '/first/{N;d}' data2 #证明确实是一行。 This is the header line. This is the last line. [qs@qs ~]$ ------ [qs@qs ~]$ cat data3 The first meeting of the Linux System Administrator's group will be held on Tuesday. All System Administrators should attend this meeting. Thank you for your attendence. [qs@qs ~]$ sed 's/System Administrator/Desktop User/' data3 #错误匹配 The first meeting of the Linux System #此处没有匹配到 Administrator's group will be held on Tuesday. All Desktop Users should attend this meeting. #此处匹配成功 Thank you for your attendence. [qs@qs ~]$ [qs@qs ~]$ sed 'N; s/System.Administrator/Desktop User/' data3 #正确匹配之一(两行合并) The first meeting of the Linux Desktop User's group will be held on Tuesday. All Desktop Users should attend this meeting. Thank you for your attendence. [qs@qs ~]$ #System 和 Administrator 之间用了通配符来匹配空格和换行。 [qs@qs ~]$ sed ' > s/System Administrator/Desktop User/ > N > s/System\nAdministrator/Desktop\nUser/ > ' data3 #正确匹配之二(行不合并,保留原来格式) (注意区分这个和下面的错误示例) The first meeting of the Linux Desktop User's group will be held on Tuesday. All Desktop Users should attend this meeting. Thank you for your attendence. [qs@qs ~]$ [qs@qs ~]$ sed ' N s/System\nAdministrator/Desktop\nUser/ s/System Administrator/Desktop User/ ' data4 #错误示例 The first meeting of the Linux Desktop User's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ 这个脚本总是在执行sed编辑器命令之前将下一行文本读入到模式空间。 当读取到最后一行的时候,因为没有下一行,所以N命令就会让sed编辑器停止。 ------- 多行删除命令 [qs@qs ~]$ cat data4 The first meeting of the Linux System Administrator's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ sed 'N; /System\nAdministrator/d' data4 #两行都被删除了 All System Administrators should attend this meeting. [qs@qs ~]$ [qs@qs ~]$ cat data4 The first meeting of the Linux System Administrator's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ sed 'N; /System\nAdministrator/D' data4 #多行删除命令D,只删除模式空间的第一行。 Administrator's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ ----- [qs@qs ~]$ cat data9 #第一行是空行 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ [qs@qs ~]$ sed '/^$/{N; /header/D}' data9 #删除从中找到数据字符串的那行的前一行 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ ------- 模式空间和保持空间 [qs@qs ~]$ cat data2 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ sed -n '/first/{ #过滤含first的行 > h #当含有first的行出现时,h命令将该行放到保持空间(This is the first data line.) > p #p命令打印模式空间也就是第一个数据行的内容 > n #n命令提取数据流的下一行(This is the second data line.), 并将它放到模式空间 > p #p命令打印模式空间的内容,现在是第二个数据行(This is the second data line.) > g #g命令将保持空间的内容(This is the first data line.)放回模式空间,替换当前文本 > p #p命令打印模式空间的内容(This is the first data line.) > }' data2 This is the first data line. This is the second data line. This is the first data line. [qs@qs ~]$ ------- 排除命令(!) [qs@qs ~]$ cat data2 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ sed -n '/header/!p' data2 #打印不包含header的行 This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ ------- [qs@qs ~]$ cat data4 The first meeting of the Linux System Administrator's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ sed 'N; s/System.Administrator/Desktop User/' data4 #sed编辑器不处理数据流中最后一行文本,因为后面没有其他行了。 The first meeting of the Linux Desktop User's group will be held on Tuesday. All System Administrators should attend this meeting. [qs@qs ~]$ sed '$!N; s/System.Administrator/Desktop User/' data4 #最后一行的时候没有执行N命令,其他的都执行了。 The first meeting of the Linux Desktop User's group will be held on Tuesday. All Desktop Users should attend this meeting. [qs@qs ~]$ ------- [qs@qs ~]$ cat data2 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ [qs@qs ~]$ sed -n '{1!G; h; $p}' data2 #翻转文本内容 This is the last line. This is the second data line. This is the first data line. This is the header line. [qs@qs ~]$ ------- [qs@qs ~]$ sed 'G' data2 #加倍行间距(最后一行也加了空行) This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ ------- [qs@qs ~]$ sed '$!G' data2 #最后一行后面不加空行 This is the header line. This is the first data line. This is the second data line. This is the last line. [qs@qs ~]$ ------- [qs@qs ~]$ cat data9 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ [qs@qs ~]$ sed '/./,$!d' data9 #删除开头的空白行 This is the header line. This is a data line. This is the last line. [qs@qs ~]$ ------- [qs@qs ~]$ cat data10 This is the first line. This is the second line. This is the third line. This is the forth line. [qs@qs ~]$ sed '/./,/^$/!d' data10 #删除连续的空白行 This is the first line. This is the second line. This is the third line. This is the forth line. [qs@qs ~]$ ------- [qs@qs ~]$ sed '=' data2 | sed 'N; s/\n/ /' #给文件中的行编号 1 This is the header line. 2 This is the first data line. 3 This is the second data line. 4 This is the last line. [qs@qs ~]$
相关文章推荐
- 个人学习笔记--sed命令(基础篇)
- DOS命令个人学习使用笔记
- linux常用命令(个人学习笔记)
- sed 命令学习笔记
- Linux学习笔记_基本命令复习(seq sed awk)
- 个人学习笔记之adb命令
- sed学习笔记二--高级命令
- sed命令学习笔记
- sed学习笔记-3(命令DPhHgGxbtnN)
- 【个人笔记】md5学习 - 命令
- linux命令sed学习笔记
- Linux Sed命令学习笔记
- sed命令 学习笔记
- LINUX系统学习笔记命令sed
- linux个人学习笔记---ubuntu14.10删除播放器历史记录命令
- shell脚本学习笔记之sed命令用法
- Linux 基本命令个人学习笔记
- Linux学习笔记_三剑客命令的sed命令复习(sed的替换功能用法)
- sed命令学习笔记
- linux常用命令(个人学习笔记)