之前批量插入代码标记 100 个文件
我想
<?php include_once('google_analytics.php'); ?>
在大约 100 个 php 文件的结束正文标记之前插入。不幸的是,制作该网站的人没有制作页眉或页脚模板。
最好的方法是什么?我尝试过使用 grep/find 获取文件列表并将结果通过 xargs 传输到 sed,但我没有运气。我的正则表达式可能是错误的。谁能帮我解决这个问题吗?
另外,您有推荐的适用于 Apple OS X 的图形工具吗?
谢谢, 迈克
编辑
find . -print0 -name "*.php" | xargs -0 perl -i.bak -pe 's/<\/body>/<?php include_once("google_analytics.php"); ?>\n<\/body>/g'
工作。
I'd like to insert
<?php include_once('google_analytics.php'); ?>
before the closing body tag of about 100 php files. Unfortunately the person who made the site didn't make a header or footer template.
What is the best way to do this? I've tried using grep/find for getting a list of files and piping the results through xargs to sed, but I've had no luck. I probably have the regex wrong. Can anyone help me out with this?
Also, are there any graphical tools for Apple OS X that you would recommend?
Thanks,
Mike
edit
find . -print0 -name "*.php" | xargs -0 perl -i.bak -pe 's/<\/body>/<?php include_once("google_analytics.php"); ?>\n<\/body>/g'
works.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
使用 sed:
-i
选项就地编辑文件。如果您说-iBAK
,那么它将在编辑之前创建文件的备份。Using sed:
The
-i
option edits the file in place. If you say-iBAK
then it will create a backup of the file before editing it.如果您对 GUI 工具感兴趣,请下载 TextMate。将所有 100 个文件放入一个文件夹中,然后用 TM 打开该文件夹。这将使 TM 进入项目模式,您将在侧边栏中看到所有文件。现在,执行
编辑>查找>在项目中查找
,将放入“查找”字段中,
在“替换”字段中,点击替换并让它运行。
If you're interested in GUI tools, download TextMate. Put all 100 files in a folder and open that folder with TM. This will put TM in project mode, and you'll see all the files in a sidebar. Now, do
Edit>Find>Find In Project
, put</body>
in the "find" field,<?php include_once('google_analytics.php'); ?></body>
in the "replace" field, hit replace and let it run.这需要一个 ed 脚本
它启动了为 Unix 编写的第一个(字面上的)程序,Ken Thompson 的
ed(1)
文本编辑器 对每个文件进行必要的编辑。如果您希望它适用于特定文件而不是目录中的每个.html
,只需将*.html
更改为"$@"
。刚才阅读维基百科链接,我学到了一些有趣的东西。 Ken Thompson 首次实际应用了正则表达式,显然它们只是一个数学表达式,直到他写了 ed(1)。
this calls for an ed script
It fires up one of the first (literally) programs ever written for Unix, Ken Thompson's
ed(1)
text editor on each file and makes the necessary edit. If you want it to work on specific files rather than on every.html
in the directory, just change*.html
to"$@"
.Reading the Wikipedia link just now, I learned something interesting. Ken Thompson made the first actual application of regular expressions, apparently they were just a mathematical expression until he wrote ed(1).
您需要在
$files
中提供一个包含文件名的数组,以使以下解决方案起作用:You need to supply an array with filenames in
$files
to make the following solution work:Dreamweaver 将对整个本地站点进行查找/替换;我相信其他 html 编辑器也会如此。
Dreamweaver will do a find/replace for the entire local site; I'm sure other html editors would as well.
某些 IDE(例如 Dreamweaver)提供在许多文件、整个文件夹等中进行查找和替换的功能。您可以使用其中之一进行查找和替换,将关闭正文标记替换为您想要的代码。
Some IDE's like dreamweaver provide functionality to do find and replace in many files, entire folders etc. You can use one of those and do a find and replace replacing the close body tag with the code you want.
我不是 PHP 开发人员,但 PHP 是有效的 XML 格式吗?看起来不像,但我知道什么。即使这个答案的成功可能更多地取决于您正在使用的文件是否是有效的 PHP。但是...如果是的话,使用 XML 转换 (xslt) 来匹配 body 标记、复制其内容并在匹配的内容后附加一个新标记将相当简单。
I'm not a PHP developer, but is PHP a valid XML format? It doesn't look like it but what do I know. Even if it is the success of this answer may depend more on whether the files you are working with are valid PHP. But...if it is it would be fairly simple to use an XML transform (xslt) to match the body tag, copy its content and append a new tag after the matched content.
替换
为
怎么样?
What about a replace on
with
?