-1

I have a text file as below

jhasdh jkhsd
lahs dkjh j

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<tbody>
<tr class="odd">
<td><p><span class="c2">sample text</span></p>
<p><span class="c2">sample text3</span></p>
</td>
</tr>
</tbody>
</table>


lajslkdjl 

;kjksalkd j

;kasdl kj


<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<tbody>
<tr class="odd">
<td><p><span class="c2">sample text223</span></p>
<p><span class="c2">sample 2332text3</span></p>
</td>
</tr>
</tbody>
</table>

jashdkjh

jashdjkh


jskdhkj

Now i want the entire <table> .mutiple lines her.. </table> to be in one line. i.e

jhasdh jkhsd
lahs dkjh j

<table><colgroup><col style="width: 100%" /></colgroup><tbody><tr class="odd"><td><p><span class="c2">sample text</span></p><p><span class="c2">sample text3</span></p></td></tr></tbody></table>


lajslkdjl 

;kjksalkd j

;kasdl kj


<table><colgroup><col style="width: 100%" /></colgroup><tbody><tr class="odd"><td><p><span class="c2">sample text223</span></p><p><span class="c2">sample 2332text3</span></p></td></tr></tbody></table>

jashdkjh

jashdjkh


jskdhkj

I can do this in sublime text as follows

find: (?s)(<table>(?:(?!<table>).)*?</table>)

then all the <table> ... </table> are selected

then find \n and replace with "" (blank) in the selection

But i want to do it using sed or awk

how can i do it

oguz ismail
  • 1
  • 16
  • 47
  • 69
Santhosh
  • 9,965
  • 20
  • 103
  • 243

2 Answers2

0
awk '/<table>/{ORS=""} /<\/table>/{ORS=RS} 1' file 

for each line:

  • if <table> is found, set Output Record Separator to empty string (""),
  • if </table> is found, restore ORS,
  • print.

see this online demo.

note that this might work for your sample but obviously won't work for all table elements. see why parsing HTML using regex is a bad practice.

oguz ismail
  • 1
  • 16
  • 47
  • 69
0

if your data in 'd' file try gnu sed,

sed -E '/<table>/{:a N;/.*\n<\/table>/!ba;s/\n//g;} ' d