简体   繁体   English

使用grep在多个文件范围内搜索字符串

[英]Searching a string using grep in a range of multiple files

Hope title is self-explanatory but I'll still try to be more clear on what I'm trying to do. 希望标题是不言自明的,但是我仍然会尽力弄清楚我要做什么。 I am looking for a string "Live message" within my log files. 我正在日志文件中寻找字符串“实时消息”。 By using simple grep command, I can get this information from all the files inside a folder. 通过使用简单的grep命令,我可以从文件夹内的所有文件中获取此信息。 The command I'm using is as follows, 我正在使用的命令如下,

grep "Live message" *

However, since I have log files ranging back to mid-last year, is there a way to define a range using grep to search for this particular string. 但是,由于我的日志文件可以追溯到去年中旬,因此有一种方法可以使用grep定义范围来搜索此特定字符串。 My log files appear as follows, 我的日志文件显示如下,

commLogs.log.2015-11-01
commLogs.log.2015-11-01
commLogs.log.2015-11-01
...
commLogs.log.2016-01-01
commLogs.log.2016-01-02
...
commLogs.log.2016-06-01
commLogs.log.2016-06-02

I would like to search for "Live message" within 2016-01-01 - 2016-06-02 range, now writing each file name would be very hard and tidious like this, 我想在2016年1月1日至2016年6月2日的范围内搜索“实时消息”,现在这样写每个文件名将变得非常艰巨和麻烦,

grep "Live message" commLogs.log.2016-01-01 commLogs.log.2016-01-02 commLogs.log.2016-01-03 ...

Is there a better way than this? 有没有比这更好的方法了?

Thank you in advance for any help 预先感谢您的任何帮助

You are fortunate that your dates are stored in YYYY-MM-DD fashion; 您很幸运,您的日期以YYYY-MM-DD格式存储; it allows you to compare dates by comparing strings lexicographically: 它允许您通过按字典顺序比较字符串来比较日期:

for f in *; do
    d=${f#commLogs.log.}
    if [[ $d > 2016-01-00 && $d < 2016-06-02 ]]; then
        cat "$f"
    fi
done | grep "Live message"

This isn't ideal; 这不理想; it's a bit verbose, and requires running cat multiple times. 这有点冗长,并且需要多次运行cat It can be improved by storing file names in an array, which will work as long as the number of matches doesn't grow too big: 可以通过将文件名存储在数组中来进行改进,只要匹配项的数量不会太大,该文件名就会起作用:

for f in *; do
    d=${f#commLogs.log.}
    if [[ $d > 2016-01-00 && $d < 2016-06-02 ]]; then
        files+=("$f")
    fi
done
grep "Live message" "${f[@]}"

Depending on the range, you may be able to write a suitable pattern to match the range, but it gets tricky since you can only pattern match strings , not numeric ranges . 根据范围,您也许可以编写一个合适的模式来匹配该范围,但是由于您只能模式匹配字符串 ,而不能对数字范围进行模式匹配,因此这很棘手。

grep "Live message" commLogs.log.2016-0[1-5]-* commLogs.log.2016-06-0[1-2]
ls * | sed "/2015-06-01/,/2016-06-03/p" -n | xargs grep "Live message"

ls * is all log file (better sort by date), may be replace on find -type f -name ... ls *是所有日志文件(最好按日期排序),可以在find -type f -name ...上替换find -type f -name ...

sed "/<BEGIN_REGEX>/,/<END_REGEX>/p" -n filter all line between BEGIN_REGEX and END_REGEX sed "/<BEGIN_REGEX>/,/<END_REGEX>/p" -n过滤BEGIN_REGEX和END_REGEX之间的所有行

xargs grep "Live message" is pass all files to grep xargs grep "Live message"将所有文件传递给grep

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM