tags:

views:

390

answers:

3

How can I insert newlines in an html file before each table related tag using sed?

A: 

This should be a basic solution

sed -s "s/<\(\/\?\)\(t\)/\n<\1\2/gi"

Not 100% perfect as it will ignore col, colgroup and catch the telytype tag but chances are you arn't using either those.

Jeremy French
+1  A: 
sed -e "s/<\\(table\\|td\\|tr\\)/\\
<\\1/gi"

Add other element names you are interested in. Not 100% perfect either: it does not cater for all of HTML weirdness, but then sed will never be enough.

kmkaplan
Don’t forget the elements THEAD, TBODY, TFOOT, CAPTION, COLGROUP, COL and TH.
Gumbo
+1  A: 

matches opening & closing tags:

sed "s/\(<\/\?\(table\|tr\|td\)\)/\n&/gi"

opening tags only:

sed "s/\(<\(table\|tr\|td\)\)/\n&/gi"

closing tags only:

sed "s/\(<\/\(table\|tr\|td\)\)/\n&/gi"
x-way