Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-01-2009, 03:11 PM   #1
LQ Newbie
Registered: Sep 2007
Posts: 27

Rep: Reputation: 15
Script to create simple html from text file

I am working on a script to convert a comma seperated text file into html code line by line.
I am a newbie with scripts (and programming in general)

The text file is like so:
link url, image url, description
and I want it to output this:
<td><a href=”link url”><img src="image url" alt=”description" /></a></td>
Here is what I have so far:
var1='<td><a href=”'
var2='”><img src="'
var3='" alt=”thumbnail image" /></a></td>'
while read line
echo $var1$line$var2$line$var3
done < gallery.txt
It puts the entire line into the html so its no good. I'll probably need to use awk (I think).
Old 10-01-2009, 03:13 PM   #2
Registered: Jan 2006
Distribution: Fedora, Ubuntu, Slackware
Posts: 56

Rep: Reputation: 15
Look into Ruby or Python, they both have a lot of libraries available specially for such tasks.
Old 10-01-2009, 03:14 PM   #3
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
tip the html generator needs to be inside the loop
thats aout all i kow about she scripts
sorry my keyboard has stuck keys
Old 10-01-2009, 03:36 PM   #4
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
This'll do it in perl

use strict;
use warnings;

# Open the input file
open (my $fh, "<", "input.txt") or die $!;

# Loop around the file performing the replacements
while (<$fh>) {
	my ($url, $image, $description) = split /,/;
	print "<td><a href=\"$url\"><img src=\"$image\" alt=\"$description\"/></a></td>\n";

close $fh;
input.txt looks like this:

link1 url,image1 url,description1
link2 url,image2 url,description2
Old 10-01-2009, 03:38 PM   #5
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Assuming it's OK to modify gallery.txt, taking out the spaces after the commas (if not that can be fixed), this should do it

while read line
    unset IFS 
    echo '<td><a href=”'"${array[0]}"'”><img src="'"${array[1]}"'" alt=”'"${array[2]}"'" /></a></td>'
done < gallery.txt
EDIT: on second thoughts this is a bit tidier

while IFS=',' read link_url image_url desc
    echo '<td><a href=”'"$link_url"'”><img src="'"$image_url"'" alt=”'"$desc"'" /></a></td>'
done < gallery.txt

Last edited by catkin; 10-01-2009 at 03:43 PM. Reason: Mark II version
Old 10-01-2009, 04:23 PM   #6
LQ Newbie
Registered: Sep 2007
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks guys.
I used the perl one. It works great!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 05:08 PM
Script or Simple App to Create HTML Table Populated With Images infidel Linux - Software 14 06-07-2005 01:08 AM
Create text file with a script zael Programming 3 06-02-2004 03:27 AM
Script to read a simple text file ravykanth Linux - Newbie 9 10-22-2003 04:15 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration