在Windows 7中使用find和sed递归修改文件

发布于 2024-11-25 09:55:55 字数 1441 浏览 3 评论 0原文

我试图在 Windows 7 中使用 find 和 GNU sed 来递归地替换跨多个目录的多个文件中的一行文本。我查看了 这个问题,但是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

我最初没有在Windows 7中使用GNU find,因为这个问题

编辑:

我尝试了以下操作,但 sed 看不到任何输入文件:

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

I'm trying to use find in Windows 7 with GNU sed to recursively replace a line of text in multiple files, across multiple directories. I looked at this question but the PowerShell solution seems to work with only one file, and I want to work with all files with a certain extension, recursively from the current directory. I tried this command:

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

But that gives me a bunch of crap and doesn't change any files:

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

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

How can I do this? It seems like I should have all the tools installed that I need, without having to install Cygwin or UnixUtils or anything else.

Edit: okay, working with GNU find, I still can't get anywhere, because I can't get the find part to work:

> 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

I was originally not using GNU find in Windows 7 because of this question.

Edit:

I tried the following, but sed doesn't see any input files this way:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

春风十里 2024-12-02 09:55:55

正在从 Windows 查找 FIND,而不是从 GNU 查找。

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

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

FIND from windows is being found instead of find from gnu.

So, rename your find.exe (from gnu) to gfind.exe (for example) and then call gfind instead of find when you wish to run it.

[edit]
gfind . -name "*.mako" (not gfind -iname "*.make" .)
[/edit]

内心荒芜 2024-12-02 09:55:55

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

You're executing the regular windows 'find' command, which has completely different command line arguments than gnu find. MS find has no capability of executing a program for each match, it simply searches.

星軌x 2024-12-02 09:55:55

Marc B/KevinDTimm 答案的补充:您的查找语法错误。

它不是:

find "*.mako"

而是:

find -name "*.mako"

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

find -name "*.mako" -type f

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

Addition to Marc B/KevinDTimm answers: your find syntax is wrong.

It is not:

find "*.mako"

but:

find -name "*.mako"

Also, if there are directories that matches "*.mako", they would be sent to sed. To avoid that:

find -name "*.mako" -type f

Finally, I think that you are missing a '\;' at the end or your find command.

坐在坟头思考人生 2024-12-02 09:55:55

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

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

使用xargs要容易得多

find . | xargs grep hello

In Powershell, note the escape sequence using backticks

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

Its much easier to use xargs

find . | xargs grep hello
原来是傀儡 2024-12-02 09:55:55

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

ls -r | grep 鲭鱼 | 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"/'

编辑:

如果 stdin 处理似乎不起作用,则解决此问题。

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

I tried the following, but sed doesn't see any input files this way:

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

With this you are now running into PowerShell's "ls" alias. Either call "ls.exe" or go all PowerShell like this:

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

Edit:

Workaround if stdin handling doesn't seem to be working.

ls -r | select-string mako -list | select -exp path | % {sed -i 's/file="layout.mako"/file="..\/layout.mako"/' $_}
雪落纷纷 2024-12-02 09:55:55

根据你的

编辑:

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

<块引用>

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

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

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

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

我希望这有帮助。

Per your

Edit:

I tried the following, but sed doesn't see any input files this way:

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

you need to use xargs to assemble the list of files passed to sed, i.e.

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

Note that for most versions of sed, you can use an alternate character to identify the substitute match/replace strings (usually '/'). Some seds require escaping that alternate char, which I have done in this example.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文