以下のファイルがあります。行が数字(好ましくは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
。標準として一度に1行ずつ繰り返します。
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'