如何在linux文件中创建空行

时间:2015-05-27 07:34:21

标签: linux file awk sed

这是我的文件,我想在每个字符现有行后创建一个空行,我知道如何使用sed / ^ $ / filename删除linux文件中的空行,我有一个像这样的大文件和我不想手动创建空行

$ cat filename
Perl 5.18 is not binary compatible with Perl 5.16 and earlier
releases. In particular, do not attempt to use extensions or PPM
packages built for ActivePerl 1600 and older series builds with
ActivePerl 1800 series builds and vice versa. Please check
"Incompatible Changes" in perl5180delta for known source level
incompatibilities between the Perl 5.18 releases and the earlier
release series

预期输出:

Perl 5.18 is not binary compatible with Perl 5.16 and earlier releases. In particular, do not attempt to 

use extensions or PPM packages built for ActivePerl 1600 and older series builds with ActivePerl 1800 

series builds and vice versa. Please check "Incompatible Changes" in perl5180delta for known source 

level incompatibilities between the Perl 5.18 releases and the earlier release series

2 个答案:

答案 0 :(得分:2)

在awk中

awk '1;NF{print ""}' file

1评估为true,因此打印该行 NF检查线上至少有一个字段(或字符),因此不会打印双空格。
{print ""}打印一个空行。

此外,如果您想在每行之间打印空行,具体取决于终端的宽度,那么您可以使用

awk -vCol="$(tput cols)" '
    {while(length($0)>Col){print substr($0,0,Col)"\n";$0=substr($0,Col+1) }}1' file

答案 1 :(得分:0)

sed '/[[:print:]]/ a\
' YourFile

或(取决于字符现有行的定义

sed '/[^[:space:][:cntrl:]]/ a\
' YourFile