在Windows 7中使用find和sed递归修改文件
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正在从 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"
(notgfind -iname "*.make" .
)[/edit]
您正在执行常规的 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.
Marc B/KevinDTimm 答案的补充:您的查找语法错误。
它不是:
而是:
另外,如果有与
"*.mako"
匹配的目录,它们将被发送到 sed。为了避免这种情况:最后,我认为您在末尾或 find 命令处缺少
'\;'
。Addition to Marc B/KevinDTimm answers: your find syntax is wrong.
It is not:
but:
Also, if there are directories that matches
"*.mako"
, they would be sent to sed. To avoid that:Finally, I think that you are missing a
'\;'
at the end or your find command.在Powershell中,注意使用反引号的转义序列
使用xargs要容易得多
In Powershell, note the escape sequence using backticks
Its much easier to use xargs
现在您将遇到 PowerShell 的“ls”别名。要么调用“ls.exe”,要么像这样使用所有 PowerShell:
编辑:
如果 stdin 处理似乎不起作用,则解决此问题。
With this you are now running into PowerShell's "ls" alias. Either call "ls.exe" or go all PowerShell like this:
Edit:
Workaround if stdin handling doesn't seem to be working.
根据你的
您需要使用 xargs 来组装传递到的文件列表sed,即
请注意,对于大多数版本的 sed,您可以使用替代字符来标识替代匹配/替换字符串(通常为 '/ ')。有些 sed 需要转义备用字符,我在本示例中已完成此操作。
我希望这有帮助。
Per your
you need to use xargs to assemble the list of files passed to sed, i.e.
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.