Home > Software design >  grep pattern in specific file
grep pattern in specific file

Time:03-19

Let's say there is file and named with every date, like…

log-2021-01-01
log-2021-01-02
log-2021-01-03
...
...
log-2021-12-31

For example: If we would like to find "fatal_error" during Jan,2021~Apr,2021, we could use following command:

#grep -E fatal_error log-2021-0[1-4]*

However, what if we want to find pattern during Sep,2021~Dec,2021? Could we achieve in one command?

We've tried a few different methods but doesn't work. Need master's advice, thanks.

CodePudding user response:

I suggest with bash:

grep fatal_error log-2021-09-* log-2021-1[0-2]-*

CodePudding user response:

grep fatal_error $(for f in log-*; do [[ $f >= log-2021-09-01 && $f <= log-2021-12-31 ]] && printf '%s\n' "$f"; done)

CodePudding user response:

Since this came up in a comment, here is a simple and portable Awk solution. The grep answer by Cyrus is much better for the OP's scenario, but perhaps this can help someone solve a more intricate one.

awk '/fatal_error/ && (FILENAME > "log-2021-09" && FILENAME < "log-2022-01")' log-*

This is somewhat inefficient; if your Awk supports nextfile, skipping to the next file (perhaps after reading the file's first line, if you want to be portable beyond GNU Awk) would potentially speed it up a lot.

CodePudding user response:

As this question is tagged :

You could use Brace Expansion:

This line will select files, from 2021-10-15 to 2022-03-30 (or now, as today is 2022-03-19).

grep fatal_error log-202{1-10-{15..31},1-1[12],2-0[1-3]}*

Let's see:

mkdir /tmp/braceDemo
cd $_
declare -i thisday
read -r thisday < <(date -d '2020-01-01'  %s)
for ((;thisday<EPOCHSECONDS;thisday =86400)) ;do
    printf -v file 'log-%(%F)T' $thisday
    touch $file
done

This will create at least 809 files, from log-2020-01-01 to log-$today...

ls -l | sed -ne '2p;$p'
-rw-r--r-- 1 user  user  0 Mar 19 10:38 log-2020-01-01
-rw-r--r-- 1 user  user  0 Mar 19 10:38 log-2022-03-19

ls | wc -l
809

Then test:

printf " - %s\n" log-202{1-10-{15..31},1-1[12],2-0[1-3]}* |
    tee >(sed -ne '1p;$p' >&2) | wc -l
 - log-2021-10-15
 - log-2022-03-19
156

This address 156 files, from log-2021-10-15 to log-2022-03-19.

  • Related