Ich brauche Informationen, welche Benutzer von meinem Proxy mit Facebook verbunden haben. Ich habe 2 Schleifen.Wie man Zähler in bash ändert, um verschiedene Zeichenketten an Datei anzuhängen
#i have format og logs, like squid.log.12.10.2017
#with `ls squid.log*` i am working with all squid logs, day by day
for i in `ls squid.log*`; do
echo "There is log $i, that we need to check"
#i am getting count of ip addresses, that were on fb.com and i am writing them to ~/temp_cache
# like this:
# 25 192.168.110.5
# 41 192.168.110.2
# where 192.168.110.5 have connected to fb.com 25 times
# and 192.168.110.2 have connected to fb.com 41 times
zgrep fb.com /var/log/$i | cut -d " " -f1 | sort | uniq -c | sort -n -k 1 >> ~/temp_cache
#i am getting only list of ip, without count of connections to facebook
# like this:
# 192.168.110.5
# 192.168.110.2
ip=$(zgrep fb.com /var/log/$i | cut -d " " -f1 | sort | uniq -c | sort -n -k 1 | awk '{print $2}')
for y in $ip; do
echo "Users from $y:"
# i have system, that we are using for projects, in this system we have log ip-addresses and logins, from these ip addresses
# like for this ip 192.168.110.5, i am getting name of user duke
# main result, like this:
# duke
# the_rock
redmine_users=$(tail -n 500000 /usr/share/redmine/log/production.log | grep -A 3 "$y" | grep "Current user" | awk '{print $3}' | head -n 1)
# i am appending to lines, name of users for these lines
# in a result it should be like this:
# 25 192.168.110.5 duke
# 41 192.168.110.2 the rock
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
done
# Delimiter for each day of logs
echo "------------------------------------------------" >> ~/temp_cache
done
Für den ersten Blick funktioniert es. Aber es funktioniert nur für einen Tag. Wenn Skript zweite Protokoll geht, ich meine squid.log.13.10.2017
, es so etwas wie dieses machen:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________ hogan
33 192.168.110.1
Aber ich möchte, dies zu tun:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________
33 192.168.110.1 hogan
Ich versuchte Skript manuell für einen Tag zu laufen, mit existsing Linie ______________________________
und
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
zu
mit wechselndenAber in Folge habe ich:
______________________________
25 192.168.110.5 duke the rock
41 192.168.110.2
Wie zu tun, was ich will, zumindest:
______________________________
25 192.168.110.5 duke
41 192.168.110.2 the rock
Wie in dieser Konstruktion Zähler ändern:
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache