LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-01-2009, 03:11 PM   #1
the_mulletator
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:
Code:
link url, image url, description
and I want it to output this:
Code:
<td><a href=”link url”><img src="image url" alt=”description" /></a></td>
Here is what I have so far:
Code:
#!/bin/bash
var1='<td><a href=”'
var2='”><img src="'
var3='" alt=”thumbnail image" /></a></td>'
while read line
do
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
cpuobsessed
Member
 
Registered: Jan 2006
Distribution: Fedora, Ubuntu, Slackware
Posts: 53

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
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,230

Rep: Reputation: 173Reputation: 173
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
gzunk
Member
 
Registered: Sep 2006
Posts: 89

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

Code:
use strict;
use warnings;

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

# Loop around the file performing the replacements
while (<$fh>) {
	chomp;
	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:

Code:
link1 url,image1 url,description1
link2 url,image2 url,description2
 
Old 10-01-2009, 03:38 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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
Code:
#!/bin/bash

while read line
do
    IFS=','
    array=($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
Code:
#!/bin/bash

while IFS=',' read link_url image_url desc
do
    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
the_mulletator
LQ Newbie
 
Registered: Sep 2007
Posts: 27

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


Reply


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


All times are GMT -5. The time now is 05:51 AM.

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