如何对"grep -l"的输出进行排序?按时间顺序,按最新修改日期为准? [英] How to sort the output of "grep -l" chronologically by newest modification date last?

查看:3098
本文介绍了如何对"grep -l"的输出进行排序?按时间顺序,按最新修改日期为准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然后我要以升序(即最新的倒数)列出结果(文件).

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

很明显

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

不是我所需要的,因为 ls -lrt 仅输出 all 个文件.

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

推荐答案

尝试:

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

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

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.

这篇关于如何对"grep -l"的输出进行排序?按时间顺序,按最新修改日期为准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆