Sed - using value returned by W+

Hi,
In a file old.txt containing (for example) a series..

 
#limerick There was a young lady from Nantucket..
\images\Blank.jpg
:end
#joke A horse walked into a bar...
\images\Blank.jpg
end

I would like to achieve new.txt containing

 
#limerick There was a young lady from Nantucket..
\images\limerick.jpg
:end
#joke A horse walked into a bar...
\images\joke.jpg
end

I can use

sed  "/#limerick/,/:end/ s/Blank/limerick/g" old.txt new.txt

and repeat similar for #joke, but as the number of #tags increases, it soon gets out of hand (and slower)

How can I use a command along the lines of

 sed  "/#\w+/,/:end/ s/Blank/value of w+ without the #/g" old.txt new.txt"  

to be able to pass the value of w+ to the replace command?

Obviously going to be a bit more complex than that! Does anyone have any ideas or solutions?

Many thanks
Nigel

awk -F"[ #]" '/^#/ {print; a=$2; getline; sub(/Blank/,a)}1' infile

Does the below sed helps..?

sed '/^#/{N;s/^#\([^ ]*\)\(.*\)\\.*$/#\1\2\\\1.jpg/}' inputfile > outfile

Thanks for taking the trouble to reply zaxxon, but I was really hoping to use sed rather than awk.

Michael, many thanks to you too - this works like a charm. Any chance of an explanation or pointing me in the direction of some further reading so I might understand what is happening!

Regards

Nigel

IMO first you would need to learn Regular Expression to govern utilities like sed,awk or perl.

Thanks - I'm reading the sed pages at grymoire at the moment, and having a play with your sed to try and gain a better understanding.

If the text varies, however, it fails:

 
#limerick There was a young lady from Nantucket..
\images\Blank.jpg
Blank
:end
#joke A horse walked into a bar,
and the barman said
\images\Blank.jpg
:end
other text #joke A bear and a rabbit.
\images\Blank.jpg
:end

to

 
#limerick There was a young lady from Nantucket..
\images\limerick.jpg
limerick
:end
#joke A horse walked into a bar,
and the barman said
\images\joke.jpg
:end
other text #joke A bear and a rabbit.
\images\joke.jpg
:end

using something like

/#\w+/,/:end/

to define a start/stop could suit my need better. Is this possible?

Nigel