sed 编辑器删除文本中的空白行
在文本处理中,一个常见的需求是删除文本中的空白行。最简单的情况是删除文本中的所有空白行。可以使用如下命令:
zzz@ubuntu:~/my_learning$ cat file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$ sed '/^$/d' file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$
删除连续的空白行
如果需要将文本中的连续多个空白行保留为仅有一行,可以使用地址区间来检查数据流。如下例,地址区间为 /./ 到 /^$/ ,区间的开始地址会匹配任何一个含有至少一个字符的非空白和和一个空白和的地址空间。
zzz@ubuntu:~/my_learning$ cat file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$ sed '/./,/^$/!d' file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$
删除文本开头的空白行
有时,数据文件开头有多个空白行,在进行数据处理时,需要删除数据流顶部的空白行。下例中,使用地址匹配 /./,$ ,匹配区间从第一个含有字符的行到文本结束都不会删除。
zzz@ubuntu:~/my_learning$ cat file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$ sed '/./,$!d' file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$
删除结尾的空白行
删除结尾的空白行,稍微复杂。可以使用如下命令:
sed '{
:start
/^\n*$/{$d; N; b start }
}'
地址模式能够匹配只含有一个换行符的行。如果匹配到这样的行,并且其还是最后一行,将使用删除命令删除。如果不是最后一行,N 命令会将下一行附加到它后面,分支命令会跳到循环起始位置重新开始。
zzz@ubuntu:~/my_learning$ cat file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$ sed '{
> :start
> /^\n*$/{$d; N; b start}
> }' file2
This is 1 line.
This is 2 line.
This is 3 line.
This is 4 line.
zzz@ubuntu:~/my_learning$