如何删除文件中的尾随空格并与前一行合并?

时间:2012-10-31 11:26:00

标签: shell unix merge

我有一个文本文件,其内容如下:

Fri Oct 02 19:52:02 +0000 2009 | JeLLz Mamii;) | DopeAssDarLey | my b.i⥠BITCH!! | @_steffhoney lmfao oh Dayum gull inee hihup Walmart en boiy maself sum skoo sepplies

Tue Aug 30 23:29:47 +0000 2011 | Braydon Tyrrell | braydon_tyrrell | westlock. AB. canada | RT @Darkskin: RT @JazmineDukes: @Darkskin Who are you that U have a blue check mark by ur name?? < 
I polish shoes at Walmart while pp ...

可以看出,“I polish shoes ..”这一行是Tues Aug 30的第二个记录的一部分。但在处理此文件时,它被视为第三个完全不同的记录。我希望能够将'I poslish shoes'系列与之前的系列合并,即“用你的名字勾选标记?”有没有办法做到这一点。这个anamoly存在于文件的许多记录中。因此我需要能够为所有这些记录执行此操作,以便我的输出看起来像

Fri Oct 02 19:52:02 +0000 2009 | JeLLz Mamii;) | DopeAssDarLey | my b.i⥠BITCH!! | @_steffhoney lmfao oh Dayum gull inee hihup Walmart en boiy maself sum skoo sepplies

Tue Aug 30 23:29:47 +0000 2011 | Braydon Tyrrell | braydon_tyrrell | westlock. AB. canada | RT @Darkskin: RT @JazmineDukes: @Darkskin Who are you that U have a blue check mark by ur name?? < I polish shoes at Walmart while pp ...

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:1)

如果它包含一个尾随空格,它将加入下一行:

sed '/ $/ N; s/\n//' file.txt

但是,如果有多行带有尾随空格,则需要一个循环:

sed -e :a -e '/ $/ { $!N; s/\n//; ta }' file

结果:

Fri Oct 02 19:52:02 +0000 2009 | JeLLz Mamii;) | DopeAssDarLey | my b.i⥠BITCH!! | @_steffhoney lmfao oh Dayum gull inee hihup Walmart en boiy maself sum skoo sepplies

Tue Aug 30 23:29:47 +0000 2011 | Braydon Tyrrell | braydon_tyrrell | westlock. AB. canada | RT @Darkskin: RT @JazmineDukes: @Darkskin Who are you that U have a blue check mark by ur name?? < I polish shoes at Walmart while pp ...

答案 1 :(得分:0)

你可以尝试:

awk -v RS='\n\n+' '{ sub(/\n/, "", $0) ; print $0 "\n" }' log.txt

答案 2 :(得分:0)

perl -00 -lpe 's/\n//g'

一次读取文件“段落”,并删除“内部”换行符。