我有一个如下所示的文件。如果该行以数字(最好是sed
或)结尾,我想删除换行符awk
。
文件.txt:
some question: 1404241
what's your name?
1498646
my name is Bond.
所以我想要如下的输出:
some question: 1404241 what's your name?
1498646 my name is Bond.
答案1
实际上给定您的输入/输出 - 您正在删除多个新行。这使得问题变得稍微困难一些,因为这样的事情sed
-作为标准一次迭代一行。
我建议使用类似 perl 的东西:
#!/usr/bin/env perl
use strict;
use warnings;
#read everything
local $/;
#do replacement of a digit, following by one - or more - linefeeds.
#m is multi-line, r is 'return the result' (to print)
#g is do it repeatedly.
print <DATA> =~ s/(\d)\n+/$1 /mrg;
__DATA__
some question: 1404241
what's your name?
1498646
my name is Bond.
这打印:
some question: 1404241 what's your name?
1498646 my name is Bond.
这可以变成一个衬垫:
perl -0777 -e 'print <> =~ s/(\d)\n+/$1 /mgr'
答案2
sed -e:n -e'$!N;s/\([0-9]\)\n/\1/;tn' -eP\;D <infile >outfile
...这样就按照您的请求进行了删除,但没有得到您请求的结果。也许您想要的是将数字后面的换行符替换为空格,对吧?
sed -e:n -e'/[0-9]$/{$!N;s/\n$//;}' -etn -e's/\n/ /' <infile >outfile
……无论如何,这应该更有效率。