-1

I am trying to append first line to the end of second line. I have huge text file where I have to this process for all the entries.

 Value="AF" /> 
<ListItem Text="Afghanistan" 


 Value="AL" />
<ListItem Text="Albania"


 Value="DZ" />
<ListItem Text="Algeria"


 Value="AS" />
<ListItem Text="American Samoa"


 Value="AD" />
<ListItem Text="Andorra"

The output should be on the lines of:

<ListItem Text="Afghanistan" Value="AF" />
<ListItem Text="Albania" Value="AL" />
<ListItem Text="Algeria" Value="DZ" />

Can anyone please help me achieve this? Thanks a lot

John Gardeniers
  • 27,458
  • 12
  • 55
  • 109
Mark
  • 101
  • 1
  • 4
  • Are there 2 blank lines between every 2 blocks? Do you want to keep it in the output? – quanta Aug 10 '12 at 09:59
  • yes.. 2 blank lines. no. it's better if they aren't present in output. even if they can't be removed, it's fine... :) – Mark Aug 10 '12 at 10:00

1 Answers1

1
$ sed -e '/^$/d' -e 'N;s/\(.*\)\n\(.*\)/\2\1/' input.txt
  • /^$/d - delete all the blank lines
  • N - put the next line into the pattern space
  • s - substitute
  • \(.*\)\n\(.*\) - matching the first line, followed by newline and the second line
  • \2\1 - append the first line to the end of second line by using back reference
quanta
  • 51,413
  • 19
  • 159
  • 217