I have a bunch of files with some old google tracking code at the bottom of the page:
我在页面底部有一堆带有旧版Google跟踪代码的文件:
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-XXXXXXXXX-1"); pageTracker._trackPageview(); } catch(err) {}</script>
I need to update that so that it has the new version of the GA code:
我需要更新它,以便它具有新版本的GA代码:
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxx-1']); _gaq.push(['_setDomainName', 'site.com']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>
Normally I would use find . -name "*html" -exec sed s/find/replace/ {} \;
to do this, but from what I understand it can't handle multi lines. How do I modify something like this to do a find and replace for multi lines, and how do I easily deal with all that stuff I'd have to escape at the command line? I'm not against creating a bash file.
通常我会使用find。 -name“* html”-exec sed s / find / replace / {} \;要做到这一点,但从我的理解,它无法处理多行。如何修改这样的东西来查找和替换多行,以及如何轻松处理我必须在命令行中逃脱的所有内容?我不反对创建一个bash文件。
I am also not against putting the "find" and "replace" stuff in text files and then pulling it into the command that way - at least that should make the escaping part easier.
我也不反对在文本文件中放入“查找”和“替换”内容,然后将其拉入命令 - 至少应该使转义部分更容易。
Thanks!
谢谢!
2 个解决方案
#1
2
I'd use perl for this.
我为此使用perl。
Something like this should get you started:
这样的事情应该让你开始:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp qw/slurp/;
my $text = slurp($ARGV[0]);
$text =~ s/foo\nbar/new_and_shiny/;
my $newfile = $ARGV[0] . ".new";
open my $out, ">$newfile" or die "$!";
print $out $text;
close $out;
Replace foo\nbar with your old javascript, and don't forget to escape all those special characters ()[] etc.
用你的旧javascript替换foo \ nbar,不要忘记逃避所有这些特殊字符()[]等。
<edit> Jim Garrison made me edit this answer and add the following:
<编辑> 吉姆加里森让我编辑这个答案并添加以下内容:
You can go one step further and replace all the google analytics javascript in your files with a single placeholder string, say 'GOOGLE_ANALYTIC_CODE' that look something like this:
您可以更进一步,使用单个占位符字符串替换文件中的所有Google Analytics分析JavaScript,例如“GOOGLE_ANALYTIC_CODE”,如下所示:
<script>
GOOGLE_ANALYTIC_CODE
</script>
and run the find-and-replace script on these files to replace the 'GOOGLE_ANALYTIC_CODE' with the latest javascript to create the 'deployed' version.
并在这些文件上运行查找和替换脚本,将“GOOGLE_ANALYTIC_CODE”替换为最新的javascript,以创建“已部署”版本。
It may take more effort now but it will definitely benefit your future self.
现在可能需要更多的努力,但它肯定会有益于你未来的自我。
This practice is very nicely documented in the The Pragmatic Programmer book when they dicuss the "DRY principle" (Dont-Repeat-Yourself).
当他们讨论“干燥原则”(Dont-Repeat-Yourself)时,这种做法很好地记录在The Pragmatic Programmer一书中。
I can't recommend that book enough. Lots and lots of good advice there.
我不能推荐那本书。那里有很多很好的建议。
</edit>
#2
0
Assuming you only have one portion of javascript and assuming you can put your new code into a file, here's a Ruby script
假设你只有一部分javascript并假设你可以把你的新代码放到一个文件中,这里是一个Ruby脚本
#!/usr/bin/env ruby
newdata = File.open("new").read
olddata = File.open("file").read
puts olddata.gsub(/<script.*<\/script>/m, newdata )
To run it,
要运行它,
$ ruby replace.rb > newfile
#1
2
I'd use perl for this.
我为此使用perl。
Something like this should get you started:
这样的事情应该让你开始:
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp qw/slurp/;
my $text = slurp($ARGV[0]);
$text =~ s/foo\nbar/new_and_shiny/;
my $newfile = $ARGV[0] . ".new";
open my $out, ">$newfile" or die "$!";
print $out $text;
close $out;
Replace foo\nbar with your old javascript, and don't forget to escape all those special characters ()[] etc.
用你的旧javascript替换foo \ nbar,不要忘记逃避所有这些特殊字符()[]等。
<edit> Jim Garrison made me edit this answer and add the following:
<编辑> 吉姆加里森让我编辑这个答案并添加以下内容:
You can go one step further and replace all the google analytics javascript in your files with a single placeholder string, say 'GOOGLE_ANALYTIC_CODE' that look something like this:
您可以更进一步,使用单个占位符字符串替换文件中的所有Google Analytics分析JavaScript,例如“GOOGLE_ANALYTIC_CODE”,如下所示:
<script>
GOOGLE_ANALYTIC_CODE
</script>
and run the find-and-replace script on these files to replace the 'GOOGLE_ANALYTIC_CODE' with the latest javascript to create the 'deployed' version.
并在这些文件上运行查找和替换脚本,将“GOOGLE_ANALYTIC_CODE”替换为最新的javascript,以创建“已部署”版本。
It may take more effort now but it will definitely benefit your future self.
现在可能需要更多的努力,但它肯定会有益于你未来的自我。
This practice is very nicely documented in the The Pragmatic Programmer book when they dicuss the "DRY principle" (Dont-Repeat-Yourself).
当他们讨论“干燥原则”(Dont-Repeat-Yourself)时,这种做法很好地记录在The Pragmatic Programmer一书中。
I can't recommend that book enough. Lots and lots of good advice there.
我不能推荐那本书。那里有很多很好的建议。
</edit>
#2
0
Assuming you only have one portion of javascript and assuming you can put your new code into a file, here's a Ruby script
假设你只有一部分javascript并假设你可以把你的新代码放到一个文件中,这里是一个Ruby脚本
#!/usr/bin/env ruby
newdata = File.open("new").read
olddata = File.open("file").read
puts olddata.gsub(/<script.*<\/script>/m, newdata )
To run it,
要运行它,
$ ruby replace.rb > newfile