Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multiple lines into one line

I have this use case of an xml file with input like

Input:
<abc a="1">
   <val>0.25</val>
</abc> 
<abc a="2">
    <val>0.25</val>
</abc> 
<abc a="3">
   <val>0.35</val>
</abc> 
 ...

Output:
<abc a="1"><val>0.25</val></abc> 
<abc a="2"><val>0.25</val></abc>
<abc a="3"><val>0.35</val></abc>

I have around 200K lines in a file in the Input format, how can I quickly convert this into output format.

like image 987
kal Avatar asked Mar 18 '10 04:03

kal


1 Answers

In vim you could do this with

:g/<abc/ .,/<\/abc/ join!

Normally :join will add a space at the end of each line before joining, but the ! suppresses that.

In general I would recommend using a proper XML parsing library in a language like Python, Ruby or Perl for manipulating XML files (I recommend Python+ElementTree), but in this case it is simple enough to get away with using a regex solution.

like image 200
Dave Kirby Avatar answered Sep 25 '22 15:09

Dave Kirby