Bir*_*rei 14
我评论了sed你不理解的命令:
sed '
## In first line: append second line with a newline character between them.
1N;
## Do the same with third line.
N;
## When found three consecutive blank lines, delete them.
## Here there are two newlines but you have to count one more deleted with last "D" command.
/^\n\n$/d;
## The combo "P+D+N" simulates a FIFO, "P+D" prints and deletes from one side while "N" appends
## a line from the other side.
P;
D
'
Run Code Online (Sandbox Code Playgroud)
删除1N,因为我们只需要两个在"叠加"线,它与第二足够多N,并改变/^\n\n$/d;以/^\n$/d;删除所有两个连续的空行.
一个测试:
内容infile:
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)
运行sed命令:
sed '
N;
/^\n$/d;
P;
D
' infile
Run Code Online (Sandbox Code Playgroud)
产量:
1
2
3
4
5
6
7
Run Code Online (Sandbox Code Playgroud)
小智 6
sed '/^$/{N;/^\n$/d;}'
Run Code Online (Sandbox Code Playgroud)
它将仅删除文件中的两个连续空行.您只能在文件中使用此表达式,然后才能完全理解.当一个空白行出现时,它将进入大括号.
通常sed会读一行.N将第二行附加到模式空间.如果该行是空行.这两行由换行符分隔.
/^\n$/这个模式将匹配那个时间只有d工作.否则d不行.d用于删除模式空间的整个内容然后开始下一个循环.