如何按mtime排序文件并传递给grep?

时间:2016-04-18 22:13:49

标签: bash grep

我尝试解析日志文件 - 从字符串中获取一些值并将其写入文件

首先,我获取按mtime排序的文件列表。

find . -name log* -printf '%Tm%Tm%Td%TH%TM%TS %p\n'| sort | awk '{print $2}'

它可以正常工作并打印文件列表

例如

./2015195/log/log.08
./2015486/log/log.10
./2015418/log/log.13
./2015415/log/log.14
./2015015/log/log.18
./2015715/log/log.19
./2015115/log/2015-09-10/log.21
...

接下来,浏览此列表并打印具有特定模式

的行中的单词
grep 'pattern' $(find . -name log* -printf '%Tm%Tm%Td%TH%TM%TS %p\n'| sort | awk '{print $2}') | awk '{print $1" "$4}' > prsd.txt

它可以工作,但它会为每个输出行添加文件名,如

./2015195/log/log.08:02:01:09,811 12345ABCD
./2015195/log/log.08:02:02:01:09,975 12345CDEF
./2015195/log/log.08:12:02:02:01:09,978 12345EFGF
./2015195/log/log.08:02:02:01:10,223 12345LJIG
./2015195/log/log.08:02:01:10,275 12345IIUY
...

这里有问题,如何删除这些添加内容?

提前感谢。

1 个答案:

答案 0 :(得分:1)

来自man grep

   -h, --no-filename
          Suppress the prefixing of file names on output. This is the 
          default when there is only one file (or only standard input)
          to search.