从 unix 终端搜索并替换多个文件中的字符串

发布于 2024-12-06 04:01:39 字数 602 浏览 1 评论 0原文

我们正在将代码库中的所有静态 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 技术交流群。

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

发布评论

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

评论(6

我做我的改变 2024-12-13 04:01:39

试试这个:

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

牛↙奶布丁 2024-12-13 04:01:39

在我的 Mac 上,我必须向 -i 选项添加一个参数:要添加到名称的扩展名(在本例中,什么都没有,因此文件存储在适当的位置)。

find . -name '*.html' -exec sed -i '' 's/\.html/\.php/g' "{}" \;

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

find . -name '*.html' -exec sed -i '' 's/\.html/\.php/g' "{}" \;
夏末的微笑 2024-12-13 04:01:39
You can certainly use ack to select your files to update. For example,
to change all "foo" to "bar" in all PHP files, you can do this from 
the Unix shell: 

perl -i -p -e's/foo/bar/g' $(ack -f --php)

来源:ma​​n ack

You can certainly use ack to select your files to update. For example,
to change all "foo" to "bar" in all PHP files, you can do this from 
the Unix shell: 

perl -i -p -e's/foo/bar/g' $(ack -f --php)

Source: man ack

往日 2024-12-13 04:01:39

这对我有用:

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'

就像说晚安 2024-12-13 04:01:39

这应该有效(在这里找到:http://www .mehtanirav.com/search-and-replace-recursively-using-sed-and-grep/):

for i in `find . -type f` ; do sed "s/html/php/g" "$i" >> "$i.xx"; mv "$i.xx" "$i"; done

This should work (found here: http://www.mehtanirav.com/search-and-replace-recursively-using-sed-and-grep/):

for i in `find . -type f` ; do sed "s/html/php/g" "$i" >> "$i.xx"; mv "$i.xx" "$i"; done
つ可否回来 2024-12-13 04:01:39

下面的脚本可以帮助替换文件中的字符串:

#!/bin/bash
echo -e "please specify the folder where string has need to be replaced: \c"
read a
echo -e "please specify the string to be replaced: \c"
read b
echo -e "please specify the string: \c"
read c
find $a -type f -exec sed -i 's/'"$b"'/'"$c"'/g' {} \; > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "string has been replaced..."
else                                                                         echo "String replacement has been failed"
exit0
fi

Below script can help in replacing the string in files:

#!/bin/bash
echo -e "please specify the folder where string has need to be replaced: \c"
read a
echo -e "please specify the string to be replaced: \c"
read b
echo -e "please specify the string: \c"
read c
find $a -type f -exec sed -i 's/'"$b"'/'"$c"'/g' {} \; > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "string has been replaced..."
else                                                                         echo "String replacement has been failed"
exit0
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文