如何过滤文件名中包含公共部分并包含一些文本的文件列表?

时间:2020-12-02 02:05:53

The following command works fine and gives me the list of files containing some text in the content -

以下命令工作正常,并为我提供了包含内容中的一些文本的文件列表 -

grep -rl "Text in content" .

The following command also works fine and gives me the list of all the files containing two text segments in the file name -

以下命令也可以正常工作,并为我提供文件名中包含两个文本段的所有文件的列表 -

ls -lrth | grep "profiling" | grep "20170714_07"

So, I tried to pipe the output of the 2nd second above to the input of the 1st command -

所以,我试图将第二秒的输出输出到第一个命令的输入 -

ls -lrth | grep "profiling" | grep "20170714_07" | grep -rl "aerospike" .

But, I am getting the following file in the output as well, which does not contain both "profiling" and "20170714_07" -

但是,我在输出中也得到以下文件,它不包含“profiling”和“20170714_07” -

./aero_debug_10_20170714.log

Please help, I am a newbie with grep.

请帮忙,我是grep的新手。

1 个解决方案

#1


0  

To do this with a single command, you'll need more than grep. If you're in a Unix environment that provides the xargs command, you could do the following:

要使用单个命令执行此操作,您需要的不仅仅是grep。如果您在提供xargs命令的Unix环境中,则可以执行以下操作:

ls -1 | grep "profiling" | grep "20170714_07" | xargs grep "aerospike"

xargs takes a list of filenames (one per line, which is why the ls commands is changed to ls -1, which lists the filenames only in that form), and then executes the command following it (grep "aerospace" in this case) for each of those files.

xargs获取一个文件名列表(每行一个,这就是ls命令更改为ls -1的原因,它仅列出该表单中的文件名),然后执行后面的命令(在本例中为grep“aerospace”)对于每个文件。

Note: if filenames have spaces in them, you might consider using a different command to solve your problem: find, with a command line similar to the following:

注意:如果文件名中包含空格,您可以考虑使用其他命令来解决问题:使用类似于以下命令行查找:

find . -name "*profiling*20170714_07*" -print0 | xargs -0 grep "aerospike"

(the only issue is that this wouldn't find filenames where the date string was first in the filename, but you could use another -name option in the find command to search for those if you needed).

(唯一的问题是,这不会找到日期字符串首先在文件名中的文件名,但是如果需要,可以在find命令中使用另一个-name选项来搜索它们)。

#1


0  

To do this with a single command, you'll need more than grep. If you're in a Unix environment that provides the xargs command, you could do the following:

要使用单个命令执行此操作,您需要的不仅仅是grep。如果您在提供xargs命令的Unix环境中,则可以执行以下操作:

ls -1 | grep "profiling" | grep "20170714_07" | xargs grep "aerospike"

xargs takes a list of filenames (one per line, which is why the ls commands is changed to ls -1, which lists the filenames only in that form), and then executes the command following it (grep "aerospace" in this case) for each of those files.

xargs获取一个文件名列表(每行一个,这就是ls命令更改为ls -1的原因,它仅列出该表单中的文件名),然后执行后面的命令(在本例中为grep“aerospace”)对于每个文件。

Note: if filenames have spaces in them, you might consider using a different command to solve your problem: find, with a command line similar to the following:

注意:如果文件名中包含空格,您可以考虑使用其他命令来解决问题:使用类似于以下命令行查找:

find . -name "*profiling*20170714_07*" -print0 | xargs -0 grep "aerospike"

(the only issue is that this wouldn't find filenames where the date string was first in the filename, but you could use another -name option in the find command to search for those if you needed).

(唯一的问题是,这不会找到日期字符串首先在文件名中的文件名,但是如果需要,可以在find命令中使用另一个-name选项来搜索它们)。