Perl pi replace all question...

how can someone replace javasript using find . perl pi command using the following example string. Can't seem to replace it no matter what i try.

im currently using the
% find . -name '*index.html' -print0 | xargs -0 perl -pi -e 's/something/something/g'

Code:
<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/* 336x280, created 1/23/10 */google_ad_slot = 
"7676767676";google_ad_width = 336;

to ...

Code:
<font size="-1">Sponsored Listings</font>
<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/* 336x280, created 1/23/10 
*/google_ad_slot = "7676767676";google_ad_width = 336;
 
Perl? Isn't that a bit of overkill for a simple substitution? Why don't you use sed(1)?
 
this is my command below....


Code:
find . -name '*index.html' -print0 | xargs -0 perl -pi -e 's/\<script type\=\"text\/javascript\"\>\<\!--google_ad_client \= \"pub-
6667767676766\"\;\/\* 336x280\, created 7\/23\/10 \*\/google_ad_slot \= \"565655555655\"\;google_ad_width \= 336\;/\<script 
type\=\"text\/javascript\"\>\<\!\--google_ad_client \= \"pub-6667767676766\"\;\/\* 336x280\, created 7\/23\/10 \*\/google_ad_slot \= 
\"565655555655\"\;google_ad_width \= 336\;/g'

but to no avail it will not work. I run the command and it appears it's working but nothing happens.
 
your last regex substitutes string with itself (at least so it lookes)
try this:

Code:
's#[color="Red"]<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/\* 336x280, created 1/23/10 \*/google_ad_slot = 
"7676767676";google_ad_width = 336;[/color]#[color="SeaGreen"]<font size="-1">Sponsored Listings</font>
<script type="text/javascript"><!--
google_ad_client = "pub-7575744747";/* 336x280, created 1/23/10 */google_ad_slot = "7676767676";google_ad_width = 336;[/color]#gi'

TIP: instead of s/// use s### or s%%% or something like that, this way you won't have to escape every / in your regex

NOTE: I added i, to ignore case... This might be important

P.S.
Sorry, line is HUGE :)
 
second replace string is different

in the second replace part im adding this extra to the string....

Code:
<font size="-1">Sponsored Listings</font>

i think the problem is
Code:
<! ---
in the javascript code
 
Code:
$ sed -e 's#[color="Red"]<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/\* 336x280, created 1/23/10 \*/google_ad_slot = 
"7676767676";google_ad_width = 336;[/color]#[color="Green"]<font size="-1">Sponsored Listings</font>
&[/color]#gI' filename

Tested, works :D

Now you owe me a Beer :)
 
i have just tried your code but did nothing. it appears it's working but nothing changes.


Code:
find . -name '*index.html' -print0 | xargs -0 perl -pi -e 's#<script type="text/javascript"><!--google_ad_client = "pub-9999999999999";/* 
336x280, created 7/23/10 */google_ad_slot = "5355545454";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
<script 
type="text/javascript"><!--google_ad_client = "pub-99999999999";/* 336x280, created 7/23/10 */google_ad_slot = "5445455454";google_ad_width = 
336;#gi'
 
here is what happened... my exact command and output

Code:
$ sed -e 's#<script type="text/javascript"><!--google_ad_client = "pub-546456546545465";/\* 336x280, created 7/23/10 \*/google_ad_slot = 
"577457457";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
<script type="text/javascript"><!--google_ad_client = "pub-
546456546545465";/\* 336x280, created 7/23/10 \*/google_ad_slot = "577457457";google_ad_width = 336;#gi'

sed: 1: "s#<script type="text/ja ...": bad flag in substitute command: 'i'
$
 
if you use sed, then it's I (big i), not i (small i) at the end of regex [#gI instead of #gi]
since I don't have gazillion of files, to modify, and I'm lazy to write them... I used single file to test regex...
thats why at the end of command (with sed) I have filename

if you use xargs you should be fine without filename :) (I think)
 
how would i convert my regular command using find and xargs to your sed xargs command?


Code:
find . -name '*index.html' -print0 | xargs -0 perl -pi -e 's/
 
either:
Code:
$ find . -name '*index.html' -print0 | xargs -0 sed -e 's#<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/\* 336x280, 
created 1/23/10 \*/google_ad_slot = "7676767676";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
&#gI'


or
make file regexfile.sed
Code:
s#<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/\* 336x280, created 1/23/10 \*/google_ad_slot = 
"7676767676";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
&#gI

and use
Code:
$ find . -name '*index.html' -print0 | xargs -0 sed -f regexfile.sed
 
I have a headache now. Stop posting unformatted stuff. Seriously.
 
this exact command does not work.

code

Code:
find . -name '*index.html' -print0 | xargs -0 sed -e 's#<script type="text/javascript"><!--google_ad_client = "pub-767676767676767";/\* 
336x280, created 7/23/10 \*/google_ad_slot = "54545454545";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
<script 
type="text/javascript"><!--google_ad_client = "pub-767676767676767";/\* 336x280, created 7/23/10 \*/google_ad_slot = 
"54545454545";google_ad_width = 336;#gI'
 
unformated stuff? sorry if i caused any grief but don't know what you mean? "unformated" you mean the way i post it? how do i post it?



DutchDaemon said:
I have a headache now. Stop posting unformatted stuff. Seriously.
 
sorry i just figured out what ya mean by unformatted stuff. i needed to place code wrap around my code text. sorry. i was wondering how he was doing that in his posts but mine would not.
 
aaaaaa, you need to add -I flag for sed
Code:
find . -name '*index.html' -print0 | xargs -0 sed [color="Red"]-I[/color] -e 's#<script type="text/javascript"><!--google_ad_client = "pub-7575744747";/\* 336x280, 
created 1/23/10 \*/google_ad_slot = "7676767676";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
&#gI'

after this check files in question, and remove backups created by sed :)

btw, is are you searching all files *index.html or all index.html? Just asking to make sure it's not a mistake in your find
 
Back
Top