extract data from html tables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extract data from html tables
# 1  
Old 03-19-2008
extract data from html tables

hi

i need to use unix to extract data from several rows of a table coded in html. I know that rows within a table have the tags <tr> </tr> and so i thought that my first step should be to to delete all of the other html code which is not contained within these tags. i could then use this method again but remove everything not in <td> </td> tags. but the big question is how can i do this? i think i need sed but at the moment it is just confusing me too much Smilie any help?
# 2  
Old 03-19-2008
In principle you are right. The following script will extract everything between a "<tr>" and "</tr>" tag. It will assume that there are no multiple "<tr>-</tr>"-pairs on a single line and the tags themselves are all lowercase (no "<TR>").

The result might not be what you need, though, so you might consider giving us a sample of what you have and what you will need to get from it. This would help us to help you better.

Code:
sed 's/.*<tr>//;s/<\/tr>.*//' /path/to/your/file

I hope this helps.

bakunin
# 3  
Old 03-19-2008
Hi.

See https://www.unix.com/shell-programmin...table-csv.html for another approach using lynx -dump.

In general, links to threads similar to yours are posted at the bottom of the thread ... cheers, drl
# 4  
Old 03-19-2008
thanks bakunin that is really helpful. i cant post a sample of the html page for various reasons. the only problem with your solution is that most of the <tr> tags are across multiple lines in my html page. ie the tag may be opened on line 7 and then closed on line 20. hence is it possible with sed to delete everything on a line (including the line) BUT stop when it gets to a <tr> tag and start again when it gets to a </tr>? alternatively is there a way to make sed believe that the whole html page is on a single line?

as i am not familiar with the capabilities of sed, it makes it hard for me to know what the best way of completing this task is.
# 5  
Old 03-19-2008
Quote:
Originally Posted by Streetrcr
thanks bakunin that is really helpful. i cant post a sample of the html page for various reasons. the only problem with your solution is that most of the <tr> tags are across multiple lines in my html page. ie the tag may be opened on line 7 and then closed on line 20. hence is it possible with sed to delete everything on a line (including the line) BUT stop when it gets to a <tr> tag and start again when it gets to a </tr>? alternatively is there a way to make sed believe that the whole html page is on a single line?

as i am not familiar with the capabilities of sed, it makes it hard for me to know what the best way of completing this task is.
There's no reason you can't mock up an HTML page which looks like the one you're working with but which does not contain any sensitive information. Nobody is interested in throwing darts into a dark room.

If you post something, someone will post code. Otherwise, you're going to have to do it yourself. Try something like replacing all newlines in the file with spaces, splitting the file before each < or after each >, and going from there. If you may have a < or > within the data, then you're going to do a little extra work. That's the best I can do for you at the moment.

ShawnMilo
# 6  
Old 03-20-2008
trying to answer my own question here, but im still struggling Smilie if this doesnt work then i will mock up an example, i just thought that my description may have been good enough withough having to waste time making an example table.

i found on this site Sed - An Introduction and Tutorial that you can create ranges by patterns. the example code is:

Code:
sed '/start/,/stop/ s/#.*//'

i tried making <tr> my start and </tr> my stop but i just kept getting errors. furthermore i would have to NOT (!) this so instead of deleting everything in the tags, it deletes everything outside the tags.
could someone please help me get this sed command working?

thanks

Last edited by Streetrcr; 03-20-2008 at 05:05 AM.. Reason: code tags
# 7  
Old 03-20-2008
Quote:
the only problem with your solution is that most of the <tr> tags are across multiple lines in my html page. ie the tag may be opened on line 7 and then closed on line 20.
Well, i told you that - in absence of any example - i had to make some assumptions. Here is a new version which will work on tags ranging over several lines. It will still not catch the case of several "<tr>...</tr>" pairs on one line, though.

Code:
sed -n '/<tr>/,/<\/tr> {
           s/.*<tr>//
           s/<\/tr>.*//
           p
           }' /path/to/your/file

How this works: the "-n" clause will stop sed from printing every line it has read, so if you delete the script it would print just nothing. This is to (implicitly) throw out all the lines which are NOT in the specified range.

Everything between the curly braces is executed only when inside the range specified on line 1. As you can see the last command inside the curly braces is a "p", which will print everything inside this range. If you delete the two "s/...."-commands it would print something this:

Code:
something....<tr> content of the tr-tag
some more content
even more content</tr> something else....

As you can see the bold parts should be deleted as they are not part of what you want. The two "s/..."-commands (s=substitute) take care of that along with the tags themselves. At last the p(rint)-command outputs the result of all the trimming.

One more word, though: You got a second answer from me because i appreciated that you were doing genuine research on your own. You almost forfeited this answer because of this:

Quote:
[...]withough having to waste time making an example table
You might notice i have "wasted time" not only writing a script but even wasted more time explaining how it works in the hope of not only solving the problem at hand but enhancing your understanding at the same time. On top of that i "wasted some more time" to write a script in my first post which nobody is going to need because it was based on faulty assumptions. Assumptions which might not have been faulty at all would i have been able to work from an example created by "wasting time".

I am even now "wasting some more time" to explain to you why you might sometimes get no answer at all or some answer you can't use. Go figure.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract the tables from html

Hi I have a script which extracts the table from HTML and convert it into .csv. But the problem in the script is if we have 2 tables in HTMl . it takes only the first table. Please help me what changes i need to do in the script to make it read the complete HTML page. Script is as below: ... (10 Replies)
Discussion started by: deepti01
10 Replies

2. Shell Programming and Scripting

Splitting csv into 3 tables in html file

I have the data in csv in 3 tables. how can I output the same into 3 tables in html.also how can I set the width. tried multiple options . attached is the format. #!/bin/ksh awk 'BEGIN{ FS="," print "<HTML><BODY><TABLE border = '1' cellpadding=10 width=100>" print... (7 Replies)
Discussion started by: archana25
7 Replies

3. Shell Programming and Scripting

awk -- Extract data from html within multiple tags as reference

Hi, I'm trying to get some data from an html file, but the problem is before it can extract the information I have multiple patterns that need to be passed through. https://www.unix.com/shell-programming-scripting/150711-extract-data-awk-html-files.html Is a similar problem. The only... (5 Replies)
Discussion started by: counfhou
5 Replies

4. Shell Programming and Scripting

extract complex data from html table rows

I have bash, awk, and sed available on my portable device. I need to extract 10 fields from each table row from a web page that looks like this: </tr> <tr> <td>28 Apr</td> <td><a... (6 Replies)
Discussion started by: rickgtx
6 Replies

5. Shell Programming and Scripting

awk to create two HTML Tables

I am working on awk script to generate an HTML format output. With input file as below I am able to generate a HTML file however I want to saperate spare devices in a different table than rest of the devices and which has only Bunch ID, RAW Size and "Bunch Spare" status columns. INPUT File : ... (2 Replies)
Discussion started by: dynamax
2 Replies

6. Shell Programming and Scripting

extract data with awk from html files

Hello everyone, I'm new to this forum and i am new as a shell scripter. my problem is to have html files in a directory and I would like to extract from these some data that lies between two different lines Here's my situation <td align="default"> oxidizability (mg / l): data_to_extract... (6 Replies)
Discussion started by: sbobotex
6 Replies

7. AIX

Extract data from DB2 tables and FTP it to outside company's firewall

Please help me in creating the script in AIX. requirement is; The new component's main function is to extract the data from DB2 tables and company's firewall directly. The component function needs to check the timestamp in the DB2 tables ((CREDAT and CRETIM) with the requested timestamp and... (1 Reply)
Discussion started by: priyanka3006
1 Replies

8. Shell Programming and Scripting

SED to extract HTML text data, not quite right!

I am attempting to extract weather data from the following website, but for the Victoria area only: Text Forecasts - Environment Canada I use this: sed -n "/Greater Victoria./,/Fraser Valley./p" But that phrasing does not sometimes get it all and think perhaps the website has more... (2 Replies)
Discussion started by: lagagnon
2 Replies

9. UNIX for Dummies Questions & Answers

How do I extract text only from html file without HTML tag

I have a html file called myfile. If I simply put "cat myfile.html" in UNIX, it shows all the html tags like <a href=r/26><img src="http://www>. But I want to extract only text part. Same problem happens in "type" command in MS-DOS. I know you can do it by opening it in Internet Explorer,... (4 Replies)
Discussion started by: los111
4 Replies

10. Shell Programming and Scripting

Converting tables of row data into columns of tables

I am trying to transpose tables listed in the format into format. Any help would be greatly appreciated. Input: test_data_1 1 2 90% 4 3 91% 5 4 90% 6 5 90% 9 6 90% test_data_2 3 5 92% 5 4 92% 7 3 93% 9 2 92% 1 1 92% ... Output:... (7 Replies)
Discussion started by: justthisguy
7 Replies
Login or Register to Ask a Question