gpt4 book ai didi

unix - 在多个文件的 shell 脚本上使用 sed

转载 作者:行者123 更新时间:2023-12-05 01:08:30 25 4
gpt4 key购买 nike

你好友好的计算人员,

这是一个两部分的问题。我正在编写两个 shell 脚本,目的是在文件夹及其子文件夹中的所有 index.php 文件中进行“查找和替换”。

第一个 shell 脚本是这样的:

echo "running on all directories…"

find . -name '*.php' -exec ./search_replace.sh {} \;

然后运行文件 search_replace :
echo "running search and replace script..."

sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' index.php > index.htm

我的问题/问题是这些:
  • 当我运行第一个脚本时,它会响应“运行搜索和替换脚本...”13 次,这是有道理的,因为文件夹中有 13 个 index.php 文件。但结果是只完成了一个文件,即替换了单词并且只创建了一次新的 htm 文件。
  • 无论如何要这样做,而不是基于 .php 文件创建一个新的 .htm 文件,我可以只使用相同的 index.php 文件替换单词?换句话说,他们可以编辑文件而不必创建新文件。

  • 非常感谢,

    最佳答案

    由于完整路径名发送到您的 search_replace.sh,您可以将 index.php 替换为 $1,将 index.htm 替换为 $1.htm,然后

    mv $1.htm $1

    作为你的 sed 之后的那一行

    回复:你的评论

    原来的
    sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' index.php > index.html

    线 s/b
    sed -e 's/http:\/\/ericbrotto.com/file:\/\/\/Users\/ericbrotto\/Documents\/Portfolio_CV_etc.\/Offline_Portfolio/g' $1 > $1.html
    mv $1.html $1

    关于unix - 在多个文件的 shell 脚本上使用 sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3923772/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com