I have this nested bash for loop where the first loop represents the range of servers, and the second loop reads each lines of the .txt file. Then will do a zgrep -i and find for a string on the matching line.
keys.txt is as follow:
1121313232323-1 1121313244234-1 1121867575757-1
Bash Loops:
for i in {1..80}; do for j in `cat keys.txt`; do zgrep -i 'InitialRequest' /var/logs/server$i/* >> /var/log/result.log; done; done &
This runs but its logging every instance where ‘InitialRequest’ is defined. What im looking for is to only log lines where the key line is matched to ‘InitialRequest’ with their key value.
The result i’m expecting is like this (with Key value from J inner loop 1121313232323-1 and with string ‘InitialRequest’ plus the remaining of the line:
1121313232323-1 InitialRequest http://someramdomurl.com
What I’m getting is like (just the matching string without the Key value from the J inner loop:
InitialRequest http://someramdomurl.com InitialRequest http://someotherramdomurl.com InitialRequest http://someotherotherramdomurl.com
Can anyone advise where is my error?
Advertisement
Answer
Your code is not zgrep
ing $j
in the files. For better clarity I will replace $i
with $server
and $j
with $key
in my answer. Check it out:
for server in server{1..80}; do for key in $(cat keys.txt); do zgrep -i "$key InitialRequest" /var/logs/$server/* >>/var/log/result.log done done
But since you are always using the same keys, you just need to read them once. Here is a better version:
keys=($(cat keys.txt)) for server in server{1..80}; do for key in ${keys[*]}; do zgrep -i "$key InitialRequest" /var/logs/$server/* >>/var/log/result.log done done