我怎样才能进行多行查找&从 *Nix 命令行替换?

发布于 2024-10-29 02:15:49 字数 1332 浏览 2 评论 0原文

我在页面底部有一堆文件,其中包含一些旧的谷歌跟踪代码:

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

我需要更新它,以便它具有新版本的 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>

通常我会使用 find 。 -name "*html" -exec sed s/find/replace/ {} \; 来做到这一点,但据我了解它无法处理多行。如何修改类似的内容以对多行进行查找和替换,以及如何轻松处理必须在命令行中转义的所有内容?我不反对创建 bash 文件。

我也不反对将“查找”和“替换”内容放入文本文件中,然后以这种方式将其拉入命令中 - 至少这应该使转义部分更容易。

谢谢!

I have a bunch of files with some old google tracking code at the bottom of the page:

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

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-11-05 02:15:49

我会为此使用 perl。

像这样的事情应该可以帮助您开始:

#!/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;

用旧的 javascript 替换 foo\nbar,并且不要忘记转义所有这些特殊字符 ()[] 等。

吉姆·加里森让我编辑这个答案并添加以下内容:

您可以更进一步,替换所有谷歌分析在文件中使用单个占位符字符串添加 javascript,例如“GOOGLE_ANALYTIC_CODE”,如下所示:

<script>
GOOGLE_ANALYTIC_CODE
</script>

并对这些文件运行查找和替换脚本,将“GOOGLE_ANALYTIC_CODE”替换为最新的 javascript,从而创建“部署”版本。

现在可能需要付出更多的努力,但绝对对未来的自己有好处。

这种做法在 The Pragmatic Programmer 一书中有很好的记录,当时他们讨论了“DRY”原则”(不要重复自己)。

我极力推荐那本书。那里有很多很多好的建议。

I'd use perl for this.

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.

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

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

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

I can't recommend that book enough. Lots and lots of good advice there.

</edit>

青衫负雪 2024-11-05 02:15:49

假设您只有 javascript 的一部分,并假设您可以将新代码放入文件中,这里有一个 Ruby 脚本

#!/usr/bin/env ruby 
newdata = File.open("new").read
olddata = File.open("file").read
puts olddata.gsub(/<script.*<\/script>/m, newdata )

来运行它,

$ ruby replace.rb > newfile 

Assuming you only have one portion of javascript and assuming you can put your new code into a file, here's a Ruby script

#!/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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文