3

I have 100 log files and I want awk to search for a given pattern between a given timestamp recursively. Log files look something like this

2010-03-24 07:00:01 ZZZZC941948879 RUFFLES 222.222.222.222 GET / - 80 - 220.181.7.113 HTTP/1.1       
2010-03-24 07:00:23 ZZZZC941948879 RUFFLES 222.222.222.222 GET 

Code is

 awk -v "b=$date1" -v "e=$date2" '$1 >= b && $1 <= e' log.txt > output
 grep -i "21things" output

I am able to search for the pattern but for single file only. Is it possible using awk command to search recursively?

Thanks for the help..!!

2
  • 3
    use find ... | xargs awk ... to pass all your logs to awk Commented Jan 4, 2015 at 20:26
  • 2
    Can you clarify what you mean by "recursive" here? It seems like you might just mean you want to run the awk command on a bunch of files. Also, why are you using -F ',' when there are no commas in the log files? Please edit your question to make it more clear what you're trying to do. Commented Jan 4, 2015 at 20:43

2 Answers 2

5

You can use find command to course through all files recursively in a directory and pass the file into awk. The following will show all the files where a match occurred. Change the <PATTERN> with required:

find . -type f -exec awk '/<PATTERN>/{print FILENAME; nextfile}' '{}' \;

The following will print out all lines matched along with filename:

find . -type f -exec awk '/<PATTERN>/{print FILENAME ":\t" $0;}' '{}' \;
Sign up to request clarification or add additional context in comments.

Comments

4

If your logs are all in the same directory, use a shell wildcard:

awk -v "b=$date1" -v "e=$date2" '$1 >= b && $1 <= e' *.log

Note that awk can do what grep does, so you don't need the temp file:

awk -v "b=$date1" -v "e=$date2" -v patt="21things" '
    $1 >= b && $1 <= e && tolower($0) ~ patt
' *.log

If you have GNU awk, use -v IGNORECASE=1 and remove the tolower function.

1 Comment

I have always used -v b="$date1", but it seems to works your way too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.