LinuxQuestions.org
Visit Jeremy's Blog.
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 09-07-2011, 02:08 AM   #1
Siljrath
Member
 
Registered: Nov 2004
Location: the internet
Distribution: sid, slack, gentoo, witch, bedrock, etc
Posts: 151

Rep: Reputation: 36
use bash and sed to generate multipage gallery from ls output


ok... i think i've reached the point where i'm stuck... time to ask. :/

i have a template html file, which i want to make a copy of for every file in the working dir.

the template has the word "filename" in it, where it needs to be replaced with the file's name.

i have the basics of a sed command that i'm confident enough will take the template, output to a new file with the appropriate filename, and replace all instances of "filename", with the required filename, currently just a placeholder variable though.

something like this:
Code:
sed s/filename/$filename/g template.html $filename.html
what i'm struggling with, is how to get the output of ls to be used, so it creates an html for/from each.

i had earlier considered starting the whole script with:

Code:
ls . > qalist && sed -i s/^/$/ qalist
but then realised i didnt have a clue where i was going from there.

my earlier version of this script generated only a single html file from the output of ls. i'm completely stumped how to get this new version to create a separate html page for each image.

eventual goal, it will also populate each page with a forward and backward button... but that's for another day.

how can i use the output of ls, to create/touch a (html)file for each (image)file listed...?

or is there some smarter simpler method of achieving my goal?

link to original single-page version of the gallery generator.
http://www.wastedartist.com/scripts/...e/qalator.html
 
Old 09-07-2011, 03:07 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,422
Blog Entries: 33

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Before I go about giving you an answer to your question I would like to ask you what you're ultimate goal for this is? If it's a class project and you're restricted to those tools then I'll certainly help you. If this is for a personal project then there's a lot easier ways of doing this such as using php or another server side language like python.

Anyway I'll wait and see what you say and then answer based on that.

Also if this is just an obsession for learning post some more information such as the contents of your template (in BB code tags). From there I think it would be easier to grasp the situation. Try to be as verbose as you can providing more information.

Last edited by sag47; 09-07-2011 at 03:17 AM.
 
Old 09-07-2011, 03:22 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
Well the first suggestion is do not use ls ... reasons found here

The simplest way forward would be to use a simple loop and have your sed act on that.
 
Old 09-07-2011, 03:27 AM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,422
Blog Entries: 33

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Quote:
Originally Posted by grail View Post
Well the first suggestion is do not use ls ... reasons found here

The simplest way forward would be to use a simple loop and have your sed act on that.
The method used by the original author doesn't encounter that problem because they're using the -m option in ls. I'm not saying it's elegant... just that it's not an issue (there's way better ways to do it). Such as sed with an inline option (or using bash for loops like you say).
 
Old 09-07-2011, 04:32 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
Quote:
The method used by the original author doesn't encounter that problem because they're using the -m option in ls.
This might be true for the original script, but he is not using -m in the example above.

May I also say that the original code on the link you provided is unnecessarily over the top. I actually see no need for either ls or sed at all.
Example:
Code:
#!/bin/bash

exec >new.html

echo '<html><body>'

for f in Pictures/email/*.jpg
do
    echo -e "\t<img src=\"$f\"><br>"
done

echo '<\body><\html>'
This created new.html which looks like:
Code:
<html><body>
	<img src="Pictures/email/drunkovision_01.jpg"><br>
	<img src="Pictures/email/drunkovision_02.jpg"><br>
	<img src="Pictures/email/drunkovision_03.jpg"><br>
---snip---
        <img src="Pictures/email/drunkovision_40.jpg"><br>
<\body><\html>
So is the idea you want now to create a single html file per picture? (Sorry I couldn't quite follow what your new requirement is)
 
Old 09-07-2011, 04:55 AM   #6
Siljrath
Member
 
Registered: Nov 2004
Location: the internet
Distribution: sid, slack, gentoo, witch, bedrock, etc
Posts: 151

Original Poster
Rep: Reputation: 36
Lightbulb "i had the boys downstairs construct a prototype, so our discussion here could have a little focus"

Quote:
Originally Posted by sag47 View Post
ultimate goal
see elaborations bellow.
Quote:
Originally Posted by sag47 View Post
restricted to those tools
a self imposed restriction to tools commonly found in linux command line (the more universally available by default, the better).
Quote:
Originally Posted by sag47 View Post
post some more information
sorry, i do go bounding along like tim robbin's character in the hudsucker proxy sometimes ("y'know, for kids").

qalator2 question clarification:
~ example code is a hasty trimmed down version to help brevity and clarity~

consider the following...
dir contains:
Code:
		1.gif
		2.gif
		3.gif
template html contains:
Code:
		<html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
		<style type="text/css">
		html, body {
		width: 100%; height: 100%; margin:0px;
		}<body
		style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(filename);"
		link="#ff0000" vlink="#0000ff" alink="#00ff00">
		filename
		</body></html>
with these^ conditions, running the script in the directory produces:
dir contains:
Code:
		1.gif
		2.gif
		3.gif
		1.html
		2.html
		3.html
1.html contains:
Code:
		<html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
		<style type="text/css">
		html, body {
		width: 100%; height: 100%; margin:0px;
		}<body
		style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(1.gif);"
		link="#ff0000" vlink="#0000ff" alink="#00ff00">
		1.gif
		</body></html>
2.html contains:
Code:
		<html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
		<style type="text/css">
		html, body {
		width: 100%; height: 100%; margin:0px;
		}<body
		style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(2.gif);"
		link="#ff0000" vlink="#0000ff" alink="#00ff00">
		2.gif
		</body></html>
3.html contains:
Code:
		<html><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
		<style type="text/css">
		html, body {
		width: 100%; height: 100%; margin:0px;
		}<body
		style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0); background-image: url(3.gif);"
		link="#ff0000" vlink="#0000ff" alink="#00ff00">
		3.gif
		</body></html>
note the two locations where it said "filename" in the template html file has been changed to the name of the files in the dir, in a correspondingly named html file for each.




trying to think it through, clearly and concisely, this following code is the jist (though no doubt bork code, and not the best way to go about it)
Code:
cp *.gif *.html
echo templatehtmlcodehere > *.html
sed -i s/filename/theactualfilesveryownfilename(minuslast5char)/g *.html
if you catch my drift?

to elaborate:
Code:
# every image is given it's own html file that it will use to be displayed in, using the same name (yes i know cp is not resource savvy choice here, i'm just conveying the jist)
cp *.gif *.html

# following on from this daft notion of using cp... we of course, need these files to contain the html code, not the image code.  again using "*" to signify all 
echo templatehtmlcodehere > *.html

# so since we've already got the files named appropriately and populated with the template html, we just need to swap the placeholder text, for the corresponding image name in all the html files.
sed -i s/filename/theactualfilesveryownfilename(minuslast5char)/g *.html

#not only is it likely inelegant to those who know better, it's also less than ideal, as this method involves including the html template code in the script itself, rather than getting it from a template file.

again, of course, this is not the smart way to go about it. i'm just using it as a rough example to get the jist of the sort of brevity and simplicity of code, and the outcome i'm aiming for.

(as you no doubt can tell) my scripting skills and knowledge arent that vast yet, and so it's a bit like trying to paint a Rembrandt when all one has in one's toolbox is a hammer.

[edit]just seen those other posts, and your suggestions grail, thnx, i'll post this and have a look, if i dont fall asleep first (why do i always get the urge to script stuff several hours after i should be sleeping?).
 
Old 09-07-2011, 07:02 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,485

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
Assuming I understand correctly:
Code:
for f in *.gif
do
    html_file=${f%.gif}.html
    sed "s/filename/$f/g" template.html > $html_file
done
 
1 members found this post helpful.
Old 09-07-2011, 03:59 PM   #8
Siljrath
Member
 
Registered: Nov 2004
Location: the internet
Distribution: sid, slack, gentoo, witch, bedrock, etc
Posts: 151

Original Poster
Rep: Reputation: 36
Thumbs up woke up this evening and found myself elated.

words cannot express the joy i felt upon seeing that. it was like the heavens parted and started playing the most holy victorious revelation glory music.

sooo clean and concise.

thnx (holy) grail.

i had only just barely learned the likes of while, for, do etc. not enough to be able to think and fluently code with loops yet, but enough to be able to read that and get it instantly.

assuming i do indeed correctly understand:
Code:
for f in *.gif
do
    html_file=${f%.gif}.html
    sed "s/filename/$f/g" template.html > $html_file
done
this thread most assuredly is getting marked solved.

grail, (again) THANKYOU!
 
  


Reply

Tags
bash, html, sed


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
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
[SOLVED] sed in a bash loop with ascending output gary_in_springhill Programming 20 01-14-2011 10:35 AM
parse input text file and generate output TsanChung Programming 5 07-27-2008 10:23 PM
generate xml from nmap output shourya21 Linux - Newbie 1 01-24-2008 08:54 AM
Trying to generate resource information and email the output. bebeslb Programming 1 10-11-2006 09:51 AM


All times are GMT -5. The time now is 12:16 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