Append new entries from bash array to a txt file -


here's situation, wrote small script generate list of ip addresses e-mail rejected:

msgid_array=($(grep ' sendmail\[' /var/log/maillog |                  egrep -v 'stat=(sent|queued|please try again later)' |                  egrep dsn='5\.[0-9]\.[0-9]' | awk '{print $6}'))    z in ${msgid_array[@]};       ip_array[x++]=$(grep $z /var/log/maillog | egrep -m 1 -o 'relay=.*' |                       egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')   done 

so looks message id's of rejected e-mails , stores them in msgid_array.

then using loop grep's maillog each message id , filters out senders ip address , stores ip's in ip_array.

now intend run each day , let parse log entries yesterday store results in separate txt file.

if have "rejected_ip_addresses=" entry in txt file, how add new ip addresses existing list?

so today run , entry looks this:

rejected_ip_adresses=1.1.1.1 2.2.2.2 

tomorrow when run array looks because same 2 senders had problems sending e-mail there 2 new ones:

ip_array=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4) 

so entry in txt should this, point being having monthly overview of problematic addresses:

rejected_ip_adresses=1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 

thanks ideas, brain refusing me.

i append entries, 1 per line, file , sort -u:

printf "%s\n" ${ip_array[@]} >> problem_ips.txt sort problem_ips.txt > tmp.txt && mv tmp.txt problem_ips.txt 

you can speed things considerably replacing loop with:

ip_array=($(printf "%s\n" ${msgid_array[@]} | grep -f - /var/log/maillog ... )) 

you might slight speed increase replacing multiple calls grep 1 call awk same operations. however, biggest gain in removing loop greps called many times.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -