PDA

View Full Version : Sed/Awk Rearrange



mastermindg
March 5th, 2012, 08:21 PM
How do I get this:

<TABLE>
<TR>
<TD></TD>
</TR>
</TABLE>

to this:

<TABLE><TR><TD></TD></TR></TABLE>

using Sed or AWK? Thanks :confused:

Lars Noodén
March 5th, 2012, 08:28 PM
Looking around I found this recipe to remove newlines:


sed ':a;N;$!ba;s/\n//g'


There are a lot of sed one-liners out there.

mastermindg
March 5th, 2012, 08:42 PM
Thanks for the quick response!

Unfortunately I always seem to oversimplify my posts :)

I have a table with many rows that looks like this:

<TABLE>
<TR>
<TD></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>

and needs to looks like this:


<TABLE>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
</TABLE>

ofnuts
March 5th, 2012, 09:23 PM
Oversimplification question: are the <TD> empty, or do they have content?

Khayyam
March 5th, 2012, 10:38 PM
Yes, as ofnuts asked ... but this should protect whatevers between '<TD></TD>' ..


awk -v RS='' '{print $1"\n"$2$3$4"\n"$5$6$7"\n"$8}' input.txt

If the input has other tags then the following will select only the '<TABLE>*</TABLE>'


awk -v RS='' '/<TABLE>/,/<\/TABLE>/{print $1"\n"$2$3$4"\n"$5$6$7"\n"$8}' input.txt

best .. khay

EDIT: actually, it won't work as expected, I solved one problem only to create another. I'll work on a possible solution.

mastermindg
March 6th, 2012, 02:49 PM
All of the tags have content including <TR><TD> and <TABLE> (though <TABLE> isn't really required for awk). Thanks!

Khayyam
March 7th, 2012, 03:02 AM
All of the tags have content including <TR><TD> and <TABLE> (though <TABLE> isn't really required for awk).

If it "isn't required for awk" then your actually not looking to do what you first asked. I suspect there is some other reason you want those tags aligned as why would they need to be ... other than it suites how your plan to parse it (a la awk).

best ... khay