如何按最新修改日期按时间顺序对“grep -l”的输出进行排序?

时间:2022-11-23 14:05:10

I'm using the -l flag with grep to just print the matching file names.

我在grep中使用-l标志来打印匹配的文件名。

I want to then list the results (files) in ascending order i.e. newest last.

我想按升序列出结果(文件),即最新的。

Obviously

明显

grep -l <pattern> *.txt | ls -rt

is not what I need, since the ls -lrt merely outputs all files.

不是我需要的,因为ls -lrt只输出所有文件。

2 个解决方案

#1


40  

Try:

尝试:

ls -rt *.txt | xargs grep -l <pattern>

We first use ls to list *.txt files and sort them by modification time (newest last), then for each entry run them through grep so we only print out files that contain the pattern.

我们首先使用ls列出* .txt文件并按修改时间(最新的最后一个)对它们进行排序,然后对于每个条目通过grep运行它们,因此我们只打印出包含该模式的文件。

#2


1  

I know this is an old question but thought of adding my suggestion for someone who is searching for the same..

我知道这是一个老问题,但想到为寻找相同内容的人添加我的建议。

for i in $(ls -rt *.txt); do grep <pattern> $i; done

for $ in $(ls -rt * .txt);做grep $ i; DONE

#1


40  

Try:

尝试:

ls -rt *.txt | xargs grep -l <pattern>

We first use ls to list *.txt files and sort them by modification time (newest last), then for each entry run them through grep so we only print out files that contain the pattern.

我们首先使用ls列出* .txt文件并按修改时间(最新的最后一个)对它们进行排序,然后对于每个条目通过grep运行它们,因此我们只打印出包含该模式的文件。

#2


1  

I know this is an old question but thought of adding my suggestion for someone who is searching for the same..

我知道这是一个老问题,但想到为寻找相同内容的人添加我的建议。

for i in $(ls -rt *.txt); do grep <pattern> $i; done

for $ in $(ls -rt * .txt);做grep $ i; DONE