0

I have a script that will search for a keyword in all the log files. It work just fine.

LOG_FILES={ "/Sandbox/logs/*" }

for file in ${LOG_FILES[@]}; do
   grep $1 $file
done

This only works for 1 keyword. What if I want to search for more then 1 keywords, say 4 or maybe even 10 keywords.

So I would enter:

./myfile.sh 120.2.1.1 Chrome 400 POST

It would check for the IP address, chrome, 400 and POST keywords in all the log files. I see on the internet, they have something like this:

egrep 'A1|A2|A3' filename

This specify that it will grep the output that is either A1 or A2 or A3 but I would like to grep the output if it is A1 AND A2 AND A3. I'm not sure how to go about doing this? Any help would be greatly appreciated.

tks

0

The simplest answer is to use a sequence of grep calls:-

grep 'A1' filename | grep 'A2' | grep 'A3'

In order to handle an independent number of parameters you will need to build a run string:-

str="cat filename"
while [ $# -gt 0 ]; do a="$a|grep '$1'"; shift; done
eval "$a"

While I generally avoid unnecessary cat calls (sorry about the pun), its use in this instance greatly simplifies the script. I haven't tested all combinations with embedded spaces in the parameters, but the above script seems to handle the quoting correctly in simple cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.