我想删除文件中的所有\n标签,但不删除\n\n标签
这是一个例子:
this
is
a test
应该:
thisis
a test
我试图操纵 using sed ':a;N;s/\n/g',但没有成功。
Perl 解决方案:
perl -pe '/./ and chomp or print "\n"' file
一种方法sed是:
sed ':a;$!{N;ba};s/\([^\n]\)\n\([^\n]\)/\1\2/g' file
thisis
a test
这可能对您有用(GNU sed):
sed ':a;$!N;/\n$/!s/\n//;ta' file
使用awk你可以试试这个:
awk '{$1=$1}1' RS="\n\n" ORS="\n\n" file
this is
a test
编辑:另一种awk变化
awk '{printf "%s ",$0} !NF {print "\n"}' file
最后一个缩短一些:
awk '{printf "%s "(!NF?"\n\n":""),$0}' file
如果你不喜欢字段之间的空间,thisis然后像这样删除空间%s:
awk '{printf "%s"(!NF?"\n\n":""),$0}' file
or
awk '{printf (!NF?RS RS:x) $0}' file