从 unix 终端搜索并替换多个文件中的字符串
我们正在将代码库中的所有静态 html 页面转换为 php 页面。第一步是将所有 .html 文件扩展名更改为 .php (我已经这样做了)。第二步是更新每个 html 页面中的所有链接以指向新的 php 页面。
(例如,在index.php中,我有指向contact.html和about-us.html的链接。现在,由于我们已将每个.html文件扩展名替换为.php,因此我们需要将contact.html更改为contact.php,并且同样,about-us.html 到 about-us.php)。
我现在想做的是在多个文件中搜索特定字符串。 (在许多文件中搜索“contact.html”,例如index.php、index2.php、index3.php等)之后,将所有这些文件中的所有“contact.html”替换为“contact.php” ”。
我对unix命令行不熟悉,到目前为止我在论坛上看到过其他人的类似问题,但不太明白哪一个可以帮助我实现我想要的。我正在使用 cygwin,如果可能的话,我需要在没有 perl 脚本的情况下解决这个问题,因为我没有安装它。我需要尝试使用 sed、grep、find 或其他任何东西。
因此,如果你们中有人认为这是重复的,请向我指出相关的帖子。感谢您抽出时间。
We are converting all the static html pages in our codebase into php pages. The first step would be to change all the .html file extension to .php (which I already did). The second step would be to update all the links within each of those html pages to point to new php pages.
(for instance, inside index.php i have links to both contact.html and about-us.html. Now since we have replaced every .html file extension to .php, we need to change contact.html to contact.php, and likewise, about-us.html to about-us.php).
what i want to do now is to search for a particular string across multiple files. (search for "contact.html" inside many files, such as index.php, index2.php, index3.php, etc etc..) after that, replace all "contact.html" in all those files with "contact.php".
I am not familiar with unix command line, and i so far have seen other people's similar questions here in the forum but not quite understand which one could help me achieve what i want. I'm using cygwin and if possible i need to solve this without perl script since i dont have it installed. i need to try using either sed, grep, find, or anything else.
So, if any of you think that this is a duplicate please point me to a relevant post out there. thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
find . -name '*.html' -exec sed -i 's/\.html/\.php/g' "{}" \;
它将查找当前目录和子目录中以
.html
,并在每个文件上运行sed
,将其中出现的任何.html
替换为.php
。请参阅http://content.hccfl.edu/pollock/unix/findcmd.htm 了解更多详情。
Try this:
find . -name '*.html' -exec sed -i 's/\.html/\.php/g' "{}" \;
It will find all files in the current and subdirectories that end in
.html
, and runsed
on each of them to replace.html
with.php
anywhere it appears within them.See http://content.hccfl.edu/pollock/unix/findcmd.htm for more details.
在我的 Mac 上,我必须向
-i
选项添加一个参数:要添加到名称的扩展名(在本例中,什么都没有,因此文件存储在适当的位置)。On my Mac, I had to add a parameter to the
-i
option: the extension to add to the name (in this case, nothing, so the file is stored in-place).来源:man ack
Source: man ack
这对我有用:
find 。 -type f -print0 | xargs -0 sed -i 's/str1/str2/g'
This worked for me :
find . -type f -print0 | xargs -0 sed -i 's/str1/str2/g'
这应该有效(在这里找到:http://www .mehtanirav.com/search-and-replace-recursively-using-sed-and-grep/):
This should work (found here: http://www.mehtanirav.com/search-and-replace-recursively-using-sed-and-grep/):
下面的脚本可以帮助替换文件中的字符串:
Below script can help in replacing the string in files: