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 ?
答案 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
时要长得多。