我怎样才能进行多行查找&从 *Nix 命令行替换?
我在页面底部有一堆文件,其中包含一些旧的谷歌跟踪代码:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会为此使用 perl。
像这样的事情应该可以帮助您开始:
用旧的 javascript 替换 foo\nbar,并且不要忘记转义所有这些特殊字符 ()[] 等。
您可以更进一步,替换所有谷歌分析在文件中使用单个占位符字符串添加 javascript,例如“GOOGLE_ANALYTIC_CODE”,如下所示:
并对这些文件运行查找和替换脚本,将“GOOGLE_ANALYTIC_CODE”替换为最新的 javascript,从而创建“部署”版本。
现在可能需要付出更多的努力,但绝对对未来的自己有好处。
这种做法在 The Pragmatic Programmer 一书中有很好的记录,当时他们讨论了“DRY”原则”(不要重复自己)。
我极力推荐那本书。那里有很多很多好的建议。
I'd use perl for this.
Something like this should get you started:
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:
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>
假设您只有 javascript 的一部分,并假设您可以将新代码放入文件中,这里有一个 Ruby 脚本
来运行它,
Assuming you only have one portion of javascript and assuming you can put your new code into a file, here's a Ruby script
To run it,