Milan Nestoroski
Milan Nestoroski

Reputation: 3

leave a new line only before a pattern

i need to reformat a file in shell, so i only leave the 'new line' before some pattern. Ex:

Input:

Europe First
Asia Second
Africa Third
Australia Fourth
Europe Sixth
Australia Seventh
Europe  Eight
America Last

Output:

Europe FirstAsia SecondAfrica ThirdAustralia Fourth
Europe SixthAustralia Seventh
Europe  EightAmerica Last

where the pattern is "Europe"

Upvotes: 0

Views: 103

Answers (3)

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed ':a;$!N;/\nEurope/!s/\n//;ta;P;D' file

Upvotes: 0

Borodin
Borodin

Reputation: 126722

Simply chomp every line and print it, preceding it with an additional newline if it starts with the pattern

perl -ne 'chomp; print "\n"  if /^Europe/; print' myfile

Upvotes: 0

ikegami
ikegami

Reputation: 385897

perl -i -0777pe's/\n(?!Europe|\z)/ /g' file

Upvotes: 1

Related Questions