How do I check if the date I input matches the time stamp a few text files

时间:2016-10-20 19:10:01

标签: bash shell unix awk sed

Using bash I input my year month and day and need to check if a text file contains that particular date and if so I need to return the text file name.

Eg:

ThreadNum: 309264
Subject: Re: math
To: aaaa1425@aol.com (aaa)
From: Doctor Douglas
TimeStamp: 03/08/2004 at 13:58:57
Sent: yes

If my input matches the the time stamp I need to print out the file name. How do I match the time stamp using bash ?

2 个答案:

答案 0 :(得分:0)

试试这个: egrep -l 'TimeStamp: 03/08/2004' file

您可以根据需要指定任意数量的文件,或者只使用*

file内容

答案 1 :(得分:0)

您可以使用find

find /path/to/directory -type f -exec grep -l 'TimeStamp: 03/08/2004' {} \;
  • -type f告诉find仅查找文件。

  • -exec grep -l告诉find调用grep实用程序。 -l参数告诉grep仅搜索文件,直到找到给定的搜索字符串,在这种情况下找到TimeStamp: 03/08/2004。如果找到匹配项,则将文件的路径名打印到stdout。

根据目录的大小,文件数量和这些文件包含的数据量,这可能需要一些时间才能完成。如果要搜索的所有文件都包含在一个目录中,那么时间将大大减少。

例如,搜索$HOME文件夹中的每个文件所需的时间比搜索范围缩小到$HOME/Documents/Text_Files时要长得多。