-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

2
  • Are there 2 blank lines between every 2 blocks? Do you want to keep it in the output?
    – quanta
    Aug 10, 2012 at 9: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, 2012 at 10:00

1 Answer 1

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
1
  • wow... really thanks a million :) you're great... :)
    – Mark
    Aug 10, 2012 at 10:12

Not the answer you're looking for? Browse other questions tagged .