我需要获取用户从我的代理连接到Facebook的信息。我有2个循环。
#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
第一眼看起来有效。但它只能工作一天。如果脚本要进入第二个日志,我的意思是squid.log.13.10.2017
,它就是这样的:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________ hogan
33 192.168.110.1
但我想这样做:
25 192.168.110.5 duke
41 192.168.110.2 the rock
______________________________
33 192.168.110.1 hogan
我尝试手动运行脚本一天,存在行______________________________
并更改
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
到
counter=1
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
但结果我有:
______________________________
25 192.168.110.5 duke the rock
41 192.168.110.2
如何做,我想要什么,至少:
______________________________
25 192.168.110.5 duke
41 192.168.110.2 the rock
如何在这种结构中改变计数器:
counter=$((counter+1))
sed -i "$counter s|$| $redmine_users |" ~/temp_cache
答案 0 :(得分:0)
如果我理解正确,你试图得到它,所以这个名字和count和ip在同一行?
假设这是正确的,您只需在添加----以及当前位置后增加计数器。
我的代码中还有其他多个问题,但我认为这回答了主要问题。如果我错过了标记,请告诉我?
答案 1 :(得分:0)
我找到了。计数器应该在第一个循环中。感谢关注