Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have to modify a document containing xml data; one modification I couldn't set up with sed.

I have to modify the following expressions from:

<Bild href_fmt="Bilder/Bildschirmfoto%202012-06_fmt.jpeg" 


<a href="http://test.server.at/media/somefolder/Bildschirmfoto%202012-06_fmt.jpeg" 
target="_blank"><img src="http://test.server.at/media/somefolder/Bildschirmfoto%202012-06_fmt.jpeg" 
alt="" width="350" height="" /></a>

My problem: I couldn't duplicate the Bildschirmfoto%202012-06_fmt.jpeg to generate the correct new entry. The HTML tag is somewhere in the line, therefore I can't duplicate the whole line...

How can I do this with sed or other Linux commands?

share|improve this question

1 Answer 1

sed 's@.*href_fmt="[^/]*/\([^"]*\)".*@<a href="http://test.server.at/media/somefolder/\1target="_blank"><img src="http://test.server.at/media/somefolder/\1" alt="" width="350" height="" /></a>@'

We need to extract image name which is inside href_fmt="Bilder/Bildschirmfoto%202012-06_fmt.jpeg" . therefore first of all we'll write pattern for this :


where .* matches anything before href_fmt="
href_fmt=" matches href_fmt=" =) [^/]*/ matches dir name Bilder/ \([^"]*\) matches our image name. \(\) is used to make group and use it as \1 in the future ".* matches the rest of the line and now we will replace everything in the line accordinog to next expression:

<a href="http://test.server.at/media/somefolder/\1target="_blank"><img src="http://test.server.at/media/somefolder/\1" alt="" width="350" height="" /></a>

where \1 equals to image name as mentioned above.

Hope now it is clear how does it work.

share|improve this answer
This looks like a working solution. Could you expand your answer to showcase it a bit more clearly? –  ephsmith Jun 28 '12 at 13:05
Added some explanation. –  rush Jun 28 '12 at 13:15
cool it works! :-) thank you!! –  Roland Jun 28 '12 at 21:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.