# Perl pi replace all question...



## freescottsthoughts (Aug 20, 2010)

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'`


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

to ...


```
<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;
```


----------



## SirDice (Aug 20, 2010)

Perl? Isn't that a bit of overkill for a simple substitution? Why don't you use sed(1)?


----------



## freescottsthoughts (Aug 20, 2010)

what would be the sed command?


----------



## freescottsthoughts (Aug 20, 2010)

i need a siple solution to get round the 
	
	



```
<!---
```
 i tried 
	
	



```
/</!/-/-
```
 and 
	
	



```
\<\!---
```

nothing i try works.


----------



## graudeejs (Aug 20, 2010)

Did you use single quotes to quote expression?


----------



## freescottsthoughts (Aug 20, 2010)

you mean like this? 
	
	



```
\<\'!'---
```


----------



## freescottsthoughts (Aug 20, 2010)

oh i know what your saying now. yes i did.


----------



## freescottsthoughts (Aug 20, 2010)

this is my command below....



```
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.


----------



## graudeejs (Aug 20, 2010)

your last regex substitutes string with itself (at least so it lookes)
try this:


```
'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


----------



## freescottsthoughts (Aug 20, 2010)

*second replace string is different*

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


```
<font size="-1">Sponsored Listings</font>
```

i think the problem is 
	
	



```
<! ---
```
 in the javascript code


----------



## freescottsthoughts (Aug 20, 2010)

going to try the # command see what happens


----------



## graudeejs (Aug 20, 2010)

```
$ 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 

Now you owe me a Beer


----------



## freescottsthoughts (Aug 20, 2010)

i have just tried your code but did nothing. it appears it's working but nothing changes.



```
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'
```


----------



## freescottsthoughts (Aug 20, 2010)

oh you used sed. let me try this.


----------



## graudeejs (Aug 20, 2010)

fist regex was for Perl, last for sed


----------



## freescottsthoughts (Aug 20, 2010)

here is what happened...  my exact command and output


```
$ 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'
$
```


----------



## freescottsthoughts (Aug 20, 2010)

i did not place  filename at the end of my command. what does that filename on the end do?


----------



## graudeejs (Aug 20, 2010)

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)


----------



## freescottsthoughts (Aug 20, 2010)

how would i convert my regular command using find and xargs to your sed xargs command?



```
find . -name '*index.html' -print0 | xargs -0 perl -pi -e 's/
```


----------



## graudeejs (Aug 20, 2010)

either:

```
$ 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

```
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

```
$ find . -name '*index.html' -print0 | xargs -0 sed -f regexfile.sed
```


----------



## DutchDaemon (Aug 20, 2010)

I have a headache now. Stop posting unformatted stuff. Seriously.


----------



## freescottsthoughts (Aug 20, 2010)

this exact command does not work.

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'
```


----------



## freescottsthoughts (Aug 20, 2010)

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.


----------



## freescottsthoughts (Aug 20, 2010)

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.


----------



## graudeejs (Aug 20, 2010)

aaaaaa, you need to add *-I* flag for sed

```
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


----------



## freescottsthoughts (Aug 20, 2010)

what i'm trying to do here.....

I want to use a sed or perl pi find command for index.html because there is 40,000 plus index files.

i want to replace this 


```
<script type="text/javascript"><!--
google_ad_client = "pub-6546456544";
/* 336x280, created 7/23/10 */
google_ad_slot = "4565464565";
google_ad_width = 336;
```

with...


```
<font size="-1">Sponsored Listings</font>
<script type="text/javascript"><!--
google_ad_client = "pub-6565466656";
/* 336x280, created 7/23/10 */
google_ad_slot = "45645654645";
google_ad_width = 336;
```


----------



## graudeejs (Aug 20, 2010)

read my reply on previous page, if you missed it


----------



## freescottsthoughts (Aug 20, 2010)

just read it going to try it now.

i notice on all yopur code you end it like this 


```
;#<font size="-1">Sponsored Listings</font>
&#gI'
```

how come you don't display the rest of the string that needs to be replaced? OR does this
code make it not repetitive in the command? will it print the rest of it out?


----------



## graudeejs (Aug 20, 2010)

in sed & is will be replaced with string that we search for

see yourself `$ echo test | sed -e 's/test/&ing/'`


----------



## freescottsthoughts (Aug 20, 2010)

just tired this exact command and no go.



```
find . -name '*index.html' -print0 | xargs -0 sed -I -e 's#<script type="text/javascript"><!--google_ad_client = "pub-4545545545655";/\* 
336x280, created 1/23/10 \*/google_ad_slot = "5464565";google_ad_width = 336;#<font size="-1">Sponsored Listings</font>
&#gI'
```


----------



## freescottsthoughts (Aug 20, 2010)

all this does is update the file time stamp for every index.html file.


----------



## graudeejs (Aug 20, 2010)

Are there newlines somewhere in string we are searching?
perhaps you can upload some 2 files for me to play with [if that's not a commercial secret]


----------



## freescottsthoughts (Aug 20, 2010)

the code looks exactly like this in each index.html page in each subdomain. google wants me to place "Sponsored links at the top of each google adsense ad in the middle of all my pages.



```
[align=center]<p>



<script type="text/javascript"><!--
google_ad_client = "pub-999999999999999";
/* 336x280, created 1/23/10 */
google_ad_slot = "55555555555";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
```


----------



## graudeejs (Aug 20, 2010)

Ok, the problem is new lines


----------



## graudeejs (Aug 20, 2010)

I have Idea how to fix this using VIM . lol
But it's too ugly to explain, would take a week, and this is not the way to do things

Right now I have no ideas that I can show, how to fix your problem, maybe something with *awk*


----------



## freescottsthoughts (Aug 20, 2010)

ya if it was all one string we would be ok.


----------



## graudeejs (Aug 20, 2010)

Read this:
http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/


----------



## freescottsthoughts (Aug 20, 2010)

good to know for the future.


----------



## graudeejs (Aug 20, 2010)

btw, all dots have to be escaped (I forgot to do that in my regex)


----------



## DutchDaemon (Aug 20, 2010)

This thread is turning into a chat that will probably not interest anyone else by now. I suggest you resort to using Private Messaging or other means of direct contact.


----------



## graudeejs (Aug 20, 2010)

fix.sed

```
1h
1!H
${
g
s#[color="Red"]<script type="text/javascript"><!--\ngoogle_ad_client = "pub-999999999999999";\n/\* 336x280, cre[B]a[/B]
ted 1/23/10 \*/\ngoogle_ad_slot = "55555555555";\ngoogle_ad_width = 336;\ngoogle_ad_height = 280;\[B]n[/B]
//-->\n</script>\n<script type="text/javascript"\nsrc="http://pagead2\.googlesyndication\.com/page[B]a[/B]
d/show_ads\.js">\n</script>[/color]#[color="SeaGreen"]<font size="-1">Sponsored Listings</font>
&[/color]#gI
p
}
```
I split regex in middle of words.... character after which I split it, I marked bold...
so when you joint lines, make sure you don't have space between 

testfile

```
aaaaaaaaaaaaaaaaaaaaaaaaaaa
[align=center]<p>



<script type="text/javascript"><!--
google_ad_client = "pub-999999999999999";
/* 336x280, created 1/23/10 */
google_ad_slot = "55555555555";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
```

`$ sed -n -f fix.sed testfile > fixedfile`

fixedfile

```
aaaaaaaaaaaaaaaaaaaaaaaaaaa
[align=center]<p>



<font size="-1">Sponsored Listings</font>
<script type="text/javascript"><!--
google_ad_client = "pub-999999999999999";
/* 336x280, created 1/23/10 */
google_ad_slot = "55555555555";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
```

so you need to run `$  find . -name '*index.html' -print0 | xargs -0 sed -I bak -n -f fix.sed`


You love me, don't you? 

P.S.
If some files are written on windows you will have to fix \n with \r\n or something like that


----------



## jalla (Aug 21, 2010)

You'd have made things easier for yourself by ignoring the bulk of the data (most of it is just printed verbatim).

A simple perl oneliner like this would be sufficient


```
perl -ne 's/<script type="text\/javascript"><\!--/<font size="-1">Sponsored Listings<\/font>
$&/; print'
```


----------



## graudeejs (Aug 21, 2010)

jalla said:
			
		

> You'd have made things easier for yourself by ignoring the bulk of the data (most of it is just printed verbatim).
> 
> A simple perl oneliner like this would be sufficient
> 
> ...



There can be many other embedded javascripts


----------



## jalla (Aug 21, 2010)

Of course, my point was the simple regexp substitution.
You have to put this into a toolchain like find .. | xargs perl -ine ...


----------

