如何使用“grep”输出的文件名作为另一个程序的参数

时间:2022-04-03 14:39:49

I have this grep command which outputs the names of files (which contains matches to some pattern), and I want to parse those files with some file-parsing program. The pipechain looks like this:

我有这个grep命令输出文件的名称(包含与某些模式的匹配),我想用一些文件解析程序解析这些文件。 pipechain看起来像这样:

grep -rl "{some-pattern}" . | {some-file-parsing-program} > a.out

How do I get those file names as command line arguments to the file-parsing program?

如何将这些文件名作为文件解析程序的命令行参数?

For example, let's say grep returns the filenames a, b, c. How do I pass the filenames so that it's as if I'm executing

例如,假设grep返回文件名a,b,c。如何传递文件名,以便它就像我正在执行一样

{some-file-parsing-program} a b c > a.out

?

2 个解决方案

#1


9  

It looks to me as though you're wanting xargs:

它看起来好像你想要xargs:

grep -rl "{some_pattern" . | xargs your-command > a.out

I'm not convinced a.out is a good output file name, but we can let that slide. The xargs command reads white-space separated file names from standard input and then invokes your-command with those names as arguments. It may need to invoke your-command several times; unless you're using GNU xargs and you specify -r, your-command will be invoked at least once, even if there are no matching file names.

我不相信a.out是一个很好的输出文件名,但我们可以让它滑动。 xargs命令从标准输入读取空格分隔的文件名,然后使用这些名称作为参数调用您的命令。它可能需要多次调用你的命令;除非您使用GNU xargs并指定-r,否则即使没有匹配的文件名,您的命令也至少会被调用一次。

Without using xargs, you could not use sed for this job. Without using xargs, using awk would be clumsy. Perl (and Python) could manage it 'trivially'; it would be easy to write the code to read file names from standard input and then process each file in turn.

如果不使用xargs,则无法将sed用于此作业。不使用xargs,使用awk会很笨拙。 Perl(和Python)可以“平凡地”管理它;编写代码以从标准输入读取文件名然后依次处理每个文件会很容易。

#2


0  

I don't know of any linux programs that cannot read from stdin. Depending on the program, the default input may be stdin or you may need to specify to use stdin by using a command line option (often - by itself). Do you have anything particular in mind?

我不知道任何无法从stdin读取的linux程序。根据程序的不同,默认输入可能是stdin,或者您可能需要通过使用命令行选项指定使用stdin(通常 - 单独使用)。你有什么特别的想法吗?

#1


9  

It looks to me as though you're wanting xargs:

它看起来好像你想要xargs:

grep -rl "{some_pattern" . | xargs your-command > a.out

I'm not convinced a.out is a good output file name, but we can let that slide. The xargs command reads white-space separated file names from standard input and then invokes your-command with those names as arguments. It may need to invoke your-command several times; unless you're using GNU xargs and you specify -r, your-command will be invoked at least once, even if there are no matching file names.

我不相信a.out是一个很好的输出文件名,但我们可以让它滑动。 xargs命令从标准输入读取空格分隔的文件名,然后使用这些名称作为参数调用您的命令。它可能需要多次调用你的命令;除非您使用GNU xargs并指定-r,否则即使没有匹配的文件名,您的命令也至少会被调用一次。

Without using xargs, you could not use sed for this job. Without using xargs, using awk would be clumsy. Perl (and Python) could manage it 'trivially'; it would be easy to write the code to read file names from standard input and then process each file in turn.

如果不使用xargs,则无法将sed用于此作业。不使用xargs,使用awk会很笨拙。 Perl(和Python)可以“平凡地”管理它;编写代码以从标准输入读取文件名然后依次处理每个文件会很容易。

#2


0  

I don't know of any linux programs that cannot read from stdin. Depending on the program, the default input may be stdin or you may need to specify to use stdin by using a command line option (often - by itself). Do you have anything particular in mind?

我不知道任何无法从stdin读取的linux程序。根据程序的不同,默认输入可能是stdin,或者您可能需要通过使用命令行选项指定使用stdin(通常 - 单独使用)。你有什么特别的想法吗?