[beyes@beyes sed]$ sed '$!N;$!D' temp.txt
hello groad
hello google
例二:删除掉重复、连续的行
测试文本内容如下:
[beyes@beyes sed]$ cat temp.txt
hello world
hello linux
hello shell
hello shell
hello groad
hello google
hello google
hello google
运行输出:
[beyes@beyes sed]$ sed '$!N; /^\\(.*\\)\\n\\1$/!P; D' temp.txt
hello world
hello linux
hello shell
hello groad
hello google
上面命令中,先读入第一行,然后判断是否已经在文件末尾,如果不是,那么再读入一行到 pattern space,然后“ /^\\(.*\\)\\n\\1$/!P” 这条命令先判断 pattern space 里的两行是否是相同内容的行,如果不是那么就打印第一条,若是,那么 D 命令将删除第一条,然后重新一轮命令循环。这样做的结果是,重复的或者是连续的行都将被合并成一行。