The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PHP: Sorting HTML table pondlife Shell Programming and Scripting 1 02-04-2008 06:55 AM
Export a HTML table to Xcel garric Shell Programming and Scripting 3 02-01-2008 05:46 AM
How do I extract text only from html file without HTML tag los111 UNIX for Dummies Questions & Answers 4 11-28-2007 04:40 AM
HELP with using a lookup table Dolph UNIX for Dummies Questions & Answers 6 03-27-2007 04:26 AM
table howeezy High Level Programming 1 09-23-2005 02:27 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-15-2007
i_priyank i_priyank is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 9
HTML table to CSV

Hi !!
I have HTML Tables through which i want to generate graphs, but for creating graphs i need the file in CSV format so can anyone can please help me in how can i convert my HTML table file to CSV format.
Thanks in Advance
  #2 (permalink)  
Old 11-15-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
try this perl script (tested with input html file containing only one html table):
Code:
#!/usr/bin/perl
# csv_from_table.pl
use strict;
my $html_file = shift;
my $csv_file  = shift;
open (F_CSV, ">", $csv_file)    or  die "Failed to write to file $csv_file : $!";
open (F_HTML, "<", $html_file)  or  die "Failed to read file $html_file : $!";
while (<F_HTML>) {
# read html file line by line
    while (m#<TD>\s*(\d+)\s*</TD>\s*(</TR>)*#gi) {
    # keep searching for numbers within TD tags, with an optional /TR tag at the end
        if (! $2) {
        # this TD is not the last TD in the TR
            print F_CSV "$1,";
              # so write comma after this number
        }
        else {
        # this is the last TD in the TR
            print F_CSV "$1\n";
              # so write newline after this number
        }
    }
}
close (F_HTML);
close (F_CSV);
run this script as:
Code:
perl csv_from_table.pl table_data.html newfile.csv
html file that i used as input (table_data.html):
Code:
<HTML>
<HEAD>
<TITLE>Table with numeric data</TITLE>
</HEAD>
<BODY>
<TABLE border="1">
  <TR> <TD>5</TD> <TD>4</TD>
 <TD>23</TD> </TR> <TR> <TD>10</TD> <TD>3</TD> <TD>24</TD> </TR>
  <TR> <TD>6</TD> <TD>12</TD> <TD>28</TD> </TR>
  <TR> <TD>17</TD> <TD>20</TD> <TD>32</TD> </TR>
</TABLE>
</BODY>
</HTML>
  #3 (permalink)  
Old 11-15-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

If you have command lynx (a text-mode browser) installed, it does a good job of removing markup tags:
Code:
% cat s1
#!/usr/bin/env sh

# @(#) s1       Demonstrate lynx -dump to eliminate html tags.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash lynx sed tr

echo

FILE=${1-data1.html}

echo " Input data:"
cat $FILE

echo
echo " Final results:"

lynx -dump $FILE |
tee t1 |
sed -e 's/^ *//' |
tr -s ' ' ','

echo
echo " Intermediate results from lynx:"
cat t1

exit 0
Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
Lynx Version 2.8.5rel.1 (04 Feb 2004)
GNU sed version 4.1.2
tr (coreutils) 5.2.1

 Input data:
<HTML>
<HEAD>
<TITLE>Table with numeric data</TITLE>
</HEAD>
<BODY>
<TABLE border="1">
  <TR> <TD>5</TD> <TD>4</TD>
 <TD>23</TD> </TR> <TR> <TD>10</TD> <TD>3</TD> <TD>24</TD> </TR>
  <TR> <TD>6</TD> <TD>12</TD> <TD>28</TD> </TR>
  <TR> <TD>17</TD> <TD>20</TD> <TD>32</TD> </TR>
</TABLE>
</BODY>
</HTML>

 Final results:

5,4,23
10,3,24
6,12,28
17,20,32

 Intermediate results from lynx:

   5  4  23
   10 3  24
   6  12 28
   17 20 32
The lynx -dump output needs only a bit of a massage to get it into CSV format. See man lynx for details ... cheers, drl
Closed Thread

Bookmarks

Tags
csv, perl, perl script, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0