LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script or other method to convert firefox bookmark file to excel (https://www.linuxquestions.org/questions/programming-9/shell-script-or-other-method-to-convert-firefox-bookmark-file-to-excel-802159/)

suse_nerd 04-15-2010 07:00 AM

Shell script or other method to convert firefox bookmark file to excel
 
I have a big file of links
I have some entries in this format:
Code:

<A HREF="http://www.....html" ADD_DATE="1271073056" LAST_MODIFIED="1271073061">18010139</A>
others like this
Code:

<a href=http://www....html>02230726</a>
The top ones are just from a firefox bookmark file.

Need to convert them into an excel spreadsheet, preferably
so they are in this format

Code:

  A          B            C
1  CODE      Hyperlink    URL
2  18010139  18010139      http://www....
3  02230726  02230726      http://www....

I realise I can easily create column B from columns A and C in Excel but columnn C does actually have to be a hyperlink if this is done in one script.

Sergei Steshenko 04-15-2010 07:03 AM

Quote:

Originally Posted by suse_nerd (Post 3936306)
I have a big file of links
I have some entries in this format:
Code:

<A HREF="http://www.....html" ADD_DATE="1271073056" LAST_MODIFIED="1271073061">18010139</A>
others like this
Code:

<a href=http://www....html>02230726</a>
The top ones are just from a firefox bookmark file.

Need to convert them into an excel spreadsheet, preferably
so they are in this format

Code:

  A          B            C
1  CODE      Hyperlink    URL
2  18010139  18010139      http://www....
3  02230726  02230726      http://www....

I realise I can easily create column B from columns A and C in Excel but columnn C does actually have to be a hyperlink if this is done in one script.

Use Perl, pick an HTML parsing module and pick an Excel writing module (or CSV one). All mentioned categories of modules exist and are well established.

suse_nerd 04-15-2010 07:26 AM

Thankyou for that, unfortunately I do not have time to do this in perl. Can you suggest where I might find this sort of tool online?

By the way this is not homework, but is actual work!

It is my own fault the files are not in the format required, need a quick fix.

PMP 04-15-2010 07:36 AM

Does this file contains these two samples only. or it contains other text as well ?
Can you assure that each line contains one link only ?
Column A and Column B will be same ?

ghostdog74 04-15-2010 08:19 AM

Saying you don't have time is just an excuse. Time is never enough for everybody, not just you. Its up to yourself to find the time. No time ? then cut down on your parties, spend 1 or 2 hrs before you sleep to learn etc. after all, its for your work right?

Back to topic, you can use Python. Assuming you are not using later version of firefox which uses json instead

Code:

import re
html=open("bookmarks.html","Ur").read()
html=re.split("</[aA]>",html)
sp=re.compile("<a href=|>",re.I|re.DOTALL|re.M)
for item in html:
    if "<a href" in item.lower():
        chunks=item.split("\n")
        whatiwant=sp.split(chunks[0].strip())
        if whatiwant[0]=="": whatiwant.pop(0)
        try: print whatiwant[0].split()[0],whatiwant[-1]
        except: pass

this only gets the data out from the html. you can use csv module to write csv after that.

PMP 04-15-2010 08:23 AM

Code:

perl -e 'while(<>){ /href="?(.*?)(".*)?>/i; my $url=$1; />(.*)</; my $code = $1; print "$code,$code,$url\n"; }' <file>

GrapefruiTgirl 04-15-2010 08:28 AM

In case this helps, regarding post #5 -- even the shiny new Firefox can/will export your bookmarks as HTML, even though it uses JSON internally. Select Bookmarks -> Organize -> Import/Backup -> Export HTML, and then you could proceed with post #5 if you like (or whatever method, if your chosen method wants to begin with an HTML-formatted file).

ghostdog74 04-15-2010 08:33 AM

Quote:

Originally Posted by PMP (Post 3936398)
Code:

perl -e 'while(<>){ /href="?(.*?)(".*)?>/i; my $url=$1; />(.*)</; my $code = $1; print "$code,$code,$url\n"; }' <file>

that doesn't work since you are not on multiline mode and you can't guarantee every anchor tags end on same line. also the -n option simulates "while(<>)"

suse_nerd 04-15-2010 10:27 AM

Thanks for your responses.

@PMP that works great, thanks!

I now have all the data in an Excel Spreadsheet.

Sergei Steshenko 04-15-2010 01:33 PM

Quote:

Originally Posted by suse_nerd (Post 3936545)
Thanks for your responses.

@PMP that works great, thanks!

I now have all the data in an Excel Spreadsheet.

Of course it works - until your input HTML file format changes. HTML is not a line oriented format.

"Programming by coincidence" (c).

PMP 04-16-2010 01:34 AM

Quote:

Originally Posted by ghostdog74 (Post 3936415)
that doesn't work since you are not on multiline mode and you can't guarantee every anchor tags end on same line. also the -n option simulates "while(<>)"

Agree, but I took the assumptions [Post #4] and posted it based on that assumptions only. Regarding -n that is something habitual :)



Quote:

Originally Posted by Sergei Steshenko (Post 3936750)
Of course it works - until your input HTML file format changes. HTML is not a line oriented format.

"Programming by coincidence" (c).


I respect your thoughts, Given the choice I would have gone for a HTML parser to do so :)

"Programming by assumption" :D


All times are GMT -5. The time now is 03:32 PM.