简体   繁体   中英

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. 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. 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,

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; 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. 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 ...

sed "/<BEGIN_REGEX>/,/<END_REGEX>/p" -n filter all line between BEGIN_REGEX and END_REGEX

xargs grep "Live message" is pass all files to grep

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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