LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding (not replacing) a pattern match with a similar pattern? (https://www.linuxquestions.org/questions/linux-newbie-8/adding-not-replacing-a-pattern-match-with-a-similar-pattern-751370/)

b-bri 08-30-2009 10:21 PM

Adding (not replacing) a pattern match with a similar pattern?
 
I'm writing a bash script to search html files, and when I find any occurrence of an img src tag like this:

<img src="123-picture-normal.jpg" alt="some random user entered text" border="0">

I want to add a second line below it that looks like:
<img src="123-picture-thumbnail.jpg" alt="some random user entered text" border="0">

All I need to do is duplicate the line but replace "normal" with "thumbnail"

Each file can have multiple img src tags with different numbered jpgs.

I have a feeling this is a job for sed, but I'm struggling with it. Any ideas?

Kenhelm 08-30-2009 11:46 PM

Try
Code:

sed '/<img src="[0-9]*-picture-normal.jpg" alt="/{
h
s/-picture-normal.jpg" alt=/-picture-thumbnail.jpg" alt=/
x
G
}'


b-bri 08-31-2009 12:36 AM

Excellent - I knew it was possible. Thanks for showing me how.


All times are GMT -5. The time now is 11:01 PM.