Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving a part of html file to another - awk

Status
Not open for further replies.

panpol

Programmer
Oct 25, 2005
7
CY
I have the next html file
<html>
....
<table class="newstable"> ---> from here
....
</table> ---> to here
...
</html>
How can i get only the text from the line <table class="newstable"> to the next </table> to another file, with the use of awk or other filters?
As a flag from start moving lines is the word “newstable” and stop moving lines the next </table> in the file.

Thanks
 
The awk way:
awk '/^<table class="newstable">/,/^<\/table>/' /path/to/file.html > another

The sed way:
sed -n '/^<table class="newstable">/,/^<\/table>/p' /path/to/file.html > another

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
IF i want to stop print the lines from the second </table>

etc

<html>
.....

<table class="newstable"> ---> from here
<tr>
<td>
<table>
.......
</table> -- first </table>
</td>
</tr>
</table> ---> to here (second </table>)
...
</html>
(start from "newstable,"find the next </table> and ignore it and stop moving lines to the next </table>)

Is the above possible?
Thanks again
 
awk '/^<table class="newstable">/,/<\/table>/' /path/to/file.html > another

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
OOps, ignore my previous post. I've missread your question.
 
BEGIN { t=-1 }
/<table class="newstable">/ { t=0 }
/<table.*>/ { t++ }
t>0
/<\/table>/ { t-- }

And how can i move the lines to another file with the above coding?

Thanks
 
Hi

Just put it between single quotes and pass it as parameter to [tt]awk[/tt]. Or save it to a file and pass that file's name to [tt]awk[/tt] in -f option. Or save it to a file, put it a shebang and give it permission to execute. Note, that in this last case the shebang will need the -f option too :
Code:
#!/usr/bin/awk -f

BEGIN { t=-1 }
/<table class="newstable">/ { t=0 }
/<table.*>/ { t++ }
t>0
/<\/table>/ { t-- }

Feherke.
 
Can you please explain me the code?How it works?

Thanks again
 
Hi

The [tt]t[/tt] variable keep the current table level, where we are.
There could be another check for safetyness, to not start incrementing levels, if not reached a newstable table yet.

[tt]BEGIN { t=-1 } [/tt] [gray]# begin with no table[/gray]
[tt]/<table class="newstable">/ { t=0 } [/tt] [gray]# from now on we are inside the desired table[/gray]
[tt]/<table.*>/ && t!=-1 { t++ } [/tt] [gray]# if begin of a table,increment level[/gray]
[tt]t>0 [/tt] [gray]# if inside tables ( default action is print current record )[/gray]
[tt]/<\/table>/ { t-- } [/tt] [gray]# if end of a table, decrement level[/gray]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top