Replace text in multiple files

Ok guys,

If anyone could help me out on this puppy I'd be very appreciative!

Here's the scenario

I have a string for example :
<img src=BLANK_IMG border=0 width=221 height=12>
or
<img src=IMG border=0 height=12 width=221 >
or anything else really....

need to basically change each one to include alt=""
<img src=IMG alt="" .....yada yada....... >

Every line I need to change is guaranteed to start with "<img" and end with a ">" (of course..) the rest is variable.

Also...for bonus points !!! Those <img tags that DO have the alt="" line already, but in a different position, I want to MOVE IT to be next to the src

eg
move
<img src=IMG border=0 height=12 width=221 alt="">
to
<img src=IMG alt="" border=0 height=12 width=221>

Any ideas?

Thanks!

See this posting with the same type of question - should start you off in the right direction.

Thanks RTM, I saw that posting but that's just a straightforward find/replace statement - I need something that uses a fair bit of logic!

If a super-human sed statement can do it then great! Otherwise it may be a case of writing a program to read word by word, character by charater until it matches a "<img>" then read ahead until ">" ...manipulate the line, then spit it out... or something like that ???......

Of course....I welcome any other *simpler* ideas! :slight_smile:

Free your mind ....

Search first for "<img src" - check out the grep/egrep/fgrep commands - any line containing that - search for your blank alt="" statement - use a sed command to remove it.

Then run through and insert your new alt="" by searching/replacing border with alt="" border

test it out on a smaller file so you can see if it all works before hitting all your htm/html files.

sed 's/ alt=""//;s/\(<img src=[^ ]* \)/\1alt="" /g'
or something like that...

Perderabo - that statement is very helpful ! Just need to exclude any line already containing alt"<any text>" ...

Can you help on this?

I don't understand the question. Post some sample data that shows what you actually want.

And clarify something: can't one line have two or more occurances of "<img src=....>"?

What I was trying to say is that I need to exclude those img src occurences which already have an alt="(some text) as they are are not all empty.

eg

<img src=BLANK IMG> = do add an alt="" (this one works)

<img src=BLANK IMG border=0 alt=""> -- remove current one and add alt="" next to blank img (This one works)

<img src=BLANK IMG border=0 alt="image blank"> -- exclude from replace

And to answer your question, the text can appear on any line any number of times, with other text/tags surrounding it.

Hopefully that's a bit clearer

Thanks!

This pushes the problem outside of what sed can do since we can have a line like:
<img src=this height=10> <img src=that height=10 alt="">
The need to do different operations on two similiar strings on the same line is just too much. Sorry for the bad news.

Good news: combine Perderabo's solution with...

sed 's/\( alt=""\)\([^>]\)\( alt="[^"]*"\)\([^>]>\)/\3\2\4/g'

Tested...

$ cat file1
<img src=BLANK border=0>
<img src=BLANK border=0 alt="">
<img src=BLANK border=0 alt="image blank">
$ sed 's/ alt=""//g;s/\(<img src=[^ >]* \)/\1alt="" /g;s/\( alt=""\)\([^>]\)\( alt="[^"]*"\)\([^>]>\)/\3\2\4/g' file1
<img src=BLANK alt="" border=0>
<img src=BLANK alt="" border=0>
<img src=BLANK alt="image blank" border=0>

I wanted a perl script to be done for Password search & replace in two files.

For Example:

Example 1)--i am having a file such as
cat /opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table

Example 2)--and i am having a other file in other location such as cat /opt/customer/home/username/CMStaging/UAT-20080304/AR/ServerFiles/opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table

the example 1 file contains the string as **REPLACE**
the example 2 file contains the live password to be replaced.

i want is it should take the live password from the example 2 file & replace that (**REPLACE**)string with the example 1 file.

can anyone help me out for some code in detailed for the same on a urgent basis..

Thanks
Samir