user1452759
user1452759

Reputation: 9460

How to remove trailing spaces in a file and merge with previous line?

I have a text file whose conntents are this:

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 ...

As can be seen the line 'I polish shoes..' is a part of the second record of Tues Aug 30. But while processing this file for something, it is taken as a third entirely different record.

I want to be able to merge the 'I polish shoes' line with the line just before it, i.e 'check mark by your name??' Is there a way to do this. This anomaly is present in many of the records in the file. So I need to be able to do it for all such records, so that my output looks something like

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 ...

Upvotes: 1

Views: 96

Answers (3)

glenn jackman
glenn jackman

Reputation: 247052

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

That reads the file a "paragraph" at a time, and removes "interior" newlines.

Upvotes: 0

Steve
Steve

Reputation: 54532

This will join the next line if it contains a trailing space:

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

If however there could be multiple lines with trailing spaces, you'll need a loop:

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

Results:

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 ...

Upvotes: 1

gvalkov
gvalkov

Reputation: 4107

You could try:

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

Upvotes: 0

Related Questions