# sed -html gallery- *static



## a59303 (Nov 20, 2011)

Hi,

I have been struggling with this problem on and off for quite some time, more recently fairly constantly.

I want a sed command (in a script) to gather image filenames from a directory and replace the instances of 'large' and 'small' in a html template document with the relevant images consecutively.  The goal is to bash script although sometimes I am unsure whether I type [cmd=]sh 'script'[/cmd] at the shell or [cmd=]bash 'script'[/cmd] it makes a difference.

I have a directory (currently /tmp) where the images are as well as the script (egg.go). The template document (egg.one) resides currently at /root/egg.one. The errors I get don't seem to mean too much to me and I just looked for a listing of errors a bit and found none.

The consistent issue is that I can (sometimes) get the image file names to replace the 'large' and 'small' words but the script (I think) just continues to write over itself until the only replacement is the last image filename.  Occasionally I get a situation where the first image filename replaces.  Most often the 'small' replacement works better (don't know why).

Unless I miss my guess I am writing the sed wrong, which is pretty much the only thing I have been changing.

```
sed s/large/$LIM/ /root/egg.one > gallery.html
```

I think that I could use the instances of "table" in the template to control how I increment but I have been unsuccessful in getting that to work;

```
sed /table,4/s/large/$LIM/ /root/egg.one > gallery.html
```


-large-

* the filenames are a list of DSCN0717.JPG like names although there are a few exceptions;  like sDSCN0717.JPG

-small-

* the filenames are more different; DSCN0988.jpg and small.jpg

For the most part these names are just for testing but I realize that the names may in some way botch my script's function.

I hope someone can help with this, or at least point me to some listings of errors and their explanations.

Thanks

a5'


```
THE SCRIPT

#!/usr/local/bin/bash


S_IMAGE=*.jpg
L_IMAGE=*.JPG
for LIM in $L_IMAGE;do
sed s/large/$LIM/ /root/egg.one > /tmp/gallery.html
# lines commented out for simplcity.
#for SIM in $S_IMAGE;do 
#sed s/small/$SIM/ /root/egg.one > /tmp/gallery.html
#done


done
```


```
THE TEMPLATE

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transistional//EN'>
<html>
<head>
<title> Gallery Page number</title>
</head>
<body>
<h1> Gallery Page 1 </h1>





<table width='80%' height='100%' border='1' cellspacing='10'
cellpadding='10>
<a href="large"><img 
scr="small" align='center' alt="small">

<table width='80%' height='100%' border='1' cellspacing='10'
cellpadding='10'>
<a href="large"><img
src="small" align='center' alt="small">

<table width='80' height='100' border='1' cellspacing='10'
cellpadding='10'>
<a href="large"><img
src="small" align='center' alt="small">




</body>
</html>
```


----------



## wblock@ (Nov 20, 2011)

As you found, that loop will continuously overwrite gallery.html, leaving only the last version in place.  It's not clear what you really are trying to do there, create one big gallery file or individual ones.  If it's individual ones, they need to have different names, maybe from a shell variable.

Use sed's -e parameter and quotes.  Also realize that s// will only replace the first instance unless you add a g after the last slash.

Finally, sed is an antique.  It's difficult to use because it doesn't understand even most basic escapes, like \n.  Perl is much more capable and easier to use, and nearly a drop-in for sed.  (The "oh, but it uses more resources" is a misdirection 99% of the time.  The most valuable resource is programmer time spent in writing, modifying, and debugging code.)


----------



## a59303 (Nov 20, 2011)

Thank you for the response. Well, say I have:

image1.JPG
image2.JPG
image3.JPG

and:

image1.jpg
image2.jpg
image3.jpg

*note:  JPG are large and jpg are small

I want to have the script consecutively place image.JPG in the first large* slot, image.jpg in the first and second small* slots and repeat until there are 8 images used, that is 8 large with their 8 small twins.  To fill one gallery page.  Then I would like for the script to proceed to create a new page and repeat.

I think that I can do the filling of the pages bit, but I can't figure out how to get the program (sed) to skip the one it has already written, and proceed to the next one or two in the case of the small slot. I would prefer to use sed, but I don't really have much experience with any language, scripting or otherwise.  I have read that sed doesn't like newline escapes and other things and that it uses BRE's by default but it seems that I may be able to do this without that stuff.

I think I am hoping to use this functionality; from man sed



> A command line with two addresses selects and inclusive range.
> This range starts with the first pattern space that matches the first
> address.  The end of the range is the next following pattern space
> that matches the second address.  If the second address is a number
> ...



Thanks,

a5'


----------



## wblock@ (Nov 20, 2011)

Hmm.  The problem is that sed is re-run each time through the loop.  The flags for the s// command lets you specify which occurrence to replace:

```
% echo "A little test of sed" | sed -e "s/l/x/2"
A littxe test of sed
```

The one-pass way to do this would be to define an array and then run sed with eight s// commands:

```
sed -e "s/large/$LIM1/1" -e "s/large/$LIM2/2" -e "s/large/$LIM3/3" ... /root/egg.one > /tmp/gallery.html
```

Then you have to deal with reading the filenames in groups of eight.

To do it in multiple passes, use a variable to remember which replacement number was done, but you can't overwrite gallery.html each time, either.  sed's -i option might help there.


----------



## a59303 (Nov 20, 2011)

How about maybe this:

```
sed -e \/table,table/s/large/$LIM/1 -e \/large/d /root/egg.one > gallery.hmtl
```
*the idea is to substitute $LIM for large between table and table (to stdout) and then delete the pattern space line with large.  At least that's what I was hoping. At least thats what I'm up to now. I'll have to see if I can wrap my mind around the command you gave me. Although, the -i option unfortunately overwrites my template, at least that's what has happened before.


Thanks,

a5'


----------



## a59303 (Dec 7, 2011)

Hi,

_I apologize if my train of thought is somewhat random, this is how I think. I have tried repeatedly to go over and check this post and the script._

I've made some progress with the script, I now can have it recognize the address.  If I can have it recognize the _time_ it ecounters the address, I think that I can get it to work.  I have commented out the second loop to make it simpler to fix and follow.  I have changed the names of the source file and target file to gal.tmplt and gal.html.  I know that if I write 
	
	



```
sed /table/,/table/s/LARGE/$FILE/3 gal.tmplt > gal.html
```
 for instance, it will only replace the 3rd instance of the REGEXP, here being, *LARGE*.  I imagine that, if that is the case, then there might be a way to make it recognize the _number_ of address-ranges it encounters.  Also; if I can have the command write to the end of the file I would be closer.  I am sure that cat will do that but I can't find the info in man cat.  *Going back over the posts I see that there was a misunderstanding; that I wanted to edit 8 entries.  When in fact I want to change 10 occurances of the word LARGE and 5 occurances of the word thumb.  Originally I had 8 _groups_ that had to be changed

gal.scrpt

```
#! bash


list="*.jpg"
LIST="*.JPG"
for FILE in $LIST; do
sed /table/,/table/s/LARGE/$FILE/ gal.tmplt > gal.html
#for file in $list; do 
#sed /table/,/table/s/thumb/$file/ gal.tmplt > gal.html
#done
done



#ToDo:
# generate small image for page thumb in location bar.
# generate Thumb images
# ----!--------imagemagick-------!---------
#
```


gal.tmplt

```
<html>
<head>
<title>gal</title>
</head>
<body>

<table border-"3">
	<tr>
		<td>
		<a href="LARGE"> <img src="thumb"
		img alt="LARGE"></a>
		</td>
	</tr>
</table>

<table border-"3">
	<tr>
		<td>
		<a href="LARGE"> <img src="thumb"
		img alt="LARGE"></a>
		</td>
	</tr>
</table>

<table border-"3">
	<tr>
		<td>
		<a href="LARGE"> <img src="thumb"
		img alt="LARGE"></a>
		</td>
	</tr>
</table>

<table border-"3">
	<tr>
		<td>
		<a href="LARGE"> <img src="thumb"
		img alt="LARGE"></a>
		</td>
	</tr>
</table>

<table border-"3">
	<tr>
		<td>
		<a href="LARGE"> <img src="thumb"
		img alt="LARGE"></a>
		</td>
	</tr>
</table>

</body>

</html>
```

I found this link to be particularly useful.  The formatting is clear.

http://linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/index.html

So, to sum up I think the operation of the script should be:

1.) read line from gal.tmplt, look for "table".
2.) recognize "table" 
3.) replace LARGE with $FILE and write to the end of gal.html
4.) find second "table" 
5.) increment some variable
6.) repeat, skipping the already read section.

I *think* that somehow the previously mentioned property that matches some _number_ of match of REGEXP will work.  So I am trying:

working gal.scrpt

```
#! bash


list="*.jpg"
LIST="*.JPG"
n=1
for FILE in $LIST; do
sed /table/,/table/s/LARGE/$FILE/$n gal.tmplt > gal.html
let n=$n+1
echo $n
#for file in $list; do 
#sed /table/,/table/s/thumb/$file/ gal.tmplt > gal.html
#done
done



#ToDo:
# generate small image for page thumb in location bar.
# generate Thumb images
# ----!--------imagemagick-------!---------
#
```

Unfortunately, my quoting is all wrong.  I have looked at the BASH man page and online at the link I provided earlier but I can't seem to get it.


additional links that may be useful to someone:

ftp://rtfm.mit.edu/pub/faqs/editor-faq/sed

http://pubs.opengroup.org/onlinepubs/007908799/xbd/re.html#tag_007_003


----------

