【问题标题】:using find and sed in Windows 7 to recursively modify files在 Windows 7 中使用 find 和 sed 递归修改文件
【发布时间】:2011-10-09 12:03:17
【问题描述】:

我正在尝试在带有 GNU sed 的 Windows 7 中使用 find 来递归地替换多个文件中的一行文本,跨多个目录。我查看了this question,但 PowerShell 解决方案似乎只适用于一个文件,我想从当前目录递归地使用具有特定扩展名的所有文件。我试过这个命令:

find "*.mako" -exec sed -i "s:<%inherit file="layout.mako"/>:<%inherit file="../layout.mako"/>:"

但这给了我一堆废话,并且不会更改任何文件:

---------- EDIT.MAKO

File not found - -EXEC
File not found - SED
File not found - -I
File not found - LAYOUT.MAKO/>:<%INHERIT FILE=../LAYOUT.MAKO/>:

我该怎么做?看来我应该安装我需要的所有工具,而无需安装 Cygwin 或 UnixUtils 或其他任何东西。

编辑:好的,使用 GNU find,我仍然无法到达任何地方,因为我无法让 find 部分工作:

> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind . -iname "*.mako"
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression
> gfind -iname "*.mako" .
C:\Program Files (x86)\GnuWin32\bin\gfind.exe: paths must precede expression

由于this question,我最初没有在 Windows 7 中使用 GNU find。

编辑:

我尝试了以下方法,但 sed 没有以这种方式看到任何输入文件:

> ls -r | grep mako | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'
sed.exe: no input files

【问题讨论】:

    标签: windows-7 powershell replace sed find


    【解决方案1】:

    正在从 windows 中查找而不是从 gnu 中查找。

    因此,请将您的 find.exe(从 gnu)重命名为 gfind.exe(例如),然后在您希望运行时调用 gfind 而不是 find。

    [编辑]
    gfind . -name "*.mako"(不是gfind -iname "*.make" .
    [/编辑]

    【讨论】:

      【解决方案2】:

      您正在执行常规的 windows 'find' 命令,该命令的命令行参数与 gnu find 完全不同。 MS find 无法为每个匹配项执行程序,它只是搜索。

      【讨论】:

        【解决方案3】:

        添加到 Marc B/KevinDTimm 答案:您的 find 语法错误。

        不是:

        find "*.mako"
        

        但是:

        find -name "*.mako"
        

        此外,如果有与"*.mako" 匹配的目录,它们将被发送到 sed。为了避免这种情况:

        find -name "*.mako" -type f
        

        最后,我认为您在末尾缺少 '\;' 或您的 find 命令。

        【讨论】:

        • 在非 *nix 系统上,终止符将是 ; 而不是 \;,因为 ; 不是 Windows 操作系统中的元字符
        【解决方案4】:

        在 Powershell 中,注意使用反引号的转义序列

        find . -type f -exec grep hello  `{`} `;
        

        使用 xargs 更容易

        find . | xargs grep hello
        

        【讨论】:

          【解决方案5】:

          我尝试了以下方法,但 sed 没有以这种方式看到任何输入文件:

          ls -r | grep mako | sed -i 's/file="layout.mako"/file="../layout.mako"/' sed.exe:没有输入文件

          有了这个,您现在会遇到 PowerShell 的“ls”别名。要么调用“ls.exe”,要么像这样使用所有 PowerShell:

          ls -r | select-string mako -list | select -exp path | sed -i 's/file="layout.mako"/file="..\/layout.mako"/'
          

          编辑:

          如果标准输入处理似乎不起作用,则解决方法。

          ls -r | select-string mako -list | select -exp path | % {sed -i 's/file="layout.mako"/file="..\/layout.mako"/' $_}
          

          【讨论】:

          • PS &gt; ls -r | select-string mako -list | sele ct -exp path | sed -i 's/file="layout.mako"/file="..\/layout.mako"/' sed.exe: no input files,而执行所有命令,但 sed 部分确实获得了正确的文件列表。
          • 我认为sed 在这里做了一些不寻常的事情。文件路径应该进入sed的标准输入。
          【解决方案6】:

          根据你的

          编辑:

          我尝试了以下方法,但 sed 没有以这种方式看到任何输入文件:

          ls -r | grep mako | sed -i 's/file="layout.mako"/file="../layout.mako"/' sed.exe: 没有输入文件

          您需要使用 xargs 来组装传递给 sed 的文件列表,即

          ls -r | grep mako | xargs sed -i 's\:file="layout.mako":file="../layout.mako":'

          请注意,对于大多数版本的 sed,您可以使用替代字符来标识替代匹配/替换字符串(通常是“/”)。某些 sed 需要转义该备用字符,我在本示例中已完成此操作。

          我希望这会有所帮助。

          【讨论】:

            猜你喜欢
            • 2011-06-15
            • 2015-09-26
            • 2015-10-28
            • 2016-07-24
            • 2011-02-08
            • 2015-05-06
            • 2015-03-19
            相关资源
            最近更新 更多