如何使用> 在 xargs 命令中?
我想找到一个 bash 命令,它可以让我 grep 目录中的每个文件,并将该 grep 的输出写入一个单独的文件。 我的猜测是做这样的事情
ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out"
,但据我所知, xargs 不喜欢双引号。 但是,如果我删除双引号,则该命令会将整个命令的输出重定向到名为 '{}'.out 的单个文件,而不是一系列单独的文件。
有谁知道使用 xargs 执行此操作的方法吗? 我只是使用这个 grep 场景作为示例来说明我的 xargs 问题,因此任何不使用 xargs 的解决方案都不适合我。
I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this
ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out"
but, as far as I know, xargs doesn't like the double-quotes. If I remove the double-quotes, however, then the command redirects the output of the entire command to a single file called '{}'.out instead of to a series of individual files.
Does anyone know of a way to do this using xargs? I just used this grep scenario as an example to illustrate my problem with xargs so any solutions that don't use xargs aren't as applicable for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要犯这样的错误:
这会在很多情况下崩溃,包括时髦的文件名,并且不可能正确引用。 您的
{}
必须始终是命令的一个完全独立的参数,以避免代码注入错误。 您需要做的是:适用于
xargs
以及find
。顺便说一句,切勿在没有
-0
选项的情况下使用 xargs(除非是非常罕见且受控的一次性交互式使用,您不担心会破坏数据)。也不解析
ls
。 曾经。 使用 globbing 或find
代替:http://mywiki.wooledge.org/ParsingLs使用
find
处理所有需要递归和带有 glob for 的简单循环其他一切:或非递归:
注意引号的正确使用。
Do not make the mistake of doing this:
This will break under a lot of conditions, including funky filenames and is impossible to quote right. Your
{}
must always be a single completely separate argument to the command to avoid code injection bugs. What you need to do, is this:Applies to
xargs
as well asfind
.By the way, never use xargs without the
-0
option (unless for very rare and controlled one-time interactive use where you aren't worried about destroying your data).Also don't parse
ls
. Ever. Use globbing orfind
instead: http://mywiki.wooledge.org/ParsingLsUse
find
for everything that needs recursion and a simple loop with a glob for everything else:or non-recursive:
Notice the proper use of quotes.
没有
xargs
的解决方案如下:...使用
xargs
也可以实现同样的效果,结果是:编辑< /em>:lhunath。
A solution without
xargs
is the following:...and the same can be done with
xargs
, it turns out:Edit: single quotes added after remark by lhunath.
我假设您的示例只是一个示例,您可能需要 > 对于其他事情。 GNU Parallel http://www.gnu.org/software/parallel/ 可能是您的救援。 只要您的文件名不包含 \n,它就不需要额外的引号:
如果您的文件名中包含 \n:
作为额外的好处,您可以并行运行作业。
观看介绍视频以了解更多信息:http://pi.dk/1
10 秒安装将尝试进行完整安装; 如果失败,则进行个人安装; 如果失败,则进行最小安装:
如果您需要将其移动到未安装 GNU Parallel 的服务器,请尝试
parallel --embed
。I assume your example is just an example and that you may need > for other things. GNU Parallel http://www.gnu.org/software/parallel/ may be your rescue. It does not need additional quoting as long as your filenames do not contain \n:
If you have filenames with \n in it:
As an added bonus you get the jobs run in parallel.
Watch the intro videos to learn more: http://pi.dk/1
The 10 seconds installation will try to do a full installation; if that fails, a personal installation; if that fails, a minimal installation:
If you need to move it to a server, that does not have GNU Parallel installed, try
parallel --embed
.实际上,这里的大多数答案并不适用于所有文件名(如果它们包含双引号和单引号),包括 lhunath 和 Stephan202 的答案。
此解决方案适用于带有单引号和双引号的文件名:
这是一个带有单引号和双引号的文件名的测试:
Actually, most of the answers here do not work with all filenames (if they contain double and single quotes), including the answer by lhunath and Stephan202.
This solution works with filenames with single and double quotes:
Here's a test with filename with both single and double quotes: