i have table with columns. In first column, i have time, when the user has logged, in second column, i have a name of user.
13:15:39 fxs1cia1qulm1lk
13:15:39 fxs1cia1qulm1lk
13:15:39 fxs1cia1qulm1lk
13:15:42 faaaa2aa11111
13:15:49 terd1sfsd11fsdf
13:15:49 terd1sfsd11fsdf
13:15:49 terd1sfsd11fsdf
13:15:59 21erdsf123sdfsdf
13:15:59 21erdsf123sdfsdf
13:15:59 21erdsf123sdfsdf
13:15:59 21erdsf123sdfsdf
13:17:50 abcasbbabadab
13:17:50 abcasbbabadab
13:17:50 abcasbbabadab
13:17:50 abcasbbabadab
13:19:19 fxs1ce1iulmla
13:19:19 fxs1ce1iulmla
13:19:19 fxs1ce1iulmla
13:20:42 faaa2a0a1111
So, what i should do. I should to make uniq uperation with this two columns, Andm if the time of user logging and the name of user are same, i should say, that user has logged more than 3 times. I have written short script:
log_file=/root/log
temp_file=/root/temp
temp_file2=/root/temp2
cat /dev/null > $temp_file
cat /dev/null > $temp_file2
cat /dev/null > $result_file
cat $log_file | awk '{print $1}' | tail -n 20 > $temp_file
cat $log_file | awk '{print $5}' | tail -n 20 > $temp_file2
for i in `uniq -c $temp_file | awk '{print $1}'`; do
for y in `uniq -c $temp_file2 | awk '{print $2}'`; do
if [ $i -gt 3 ] && [ $y -gt 3 ]; then
s=`uniq -c $temp_file2 | awk '$1 == '$i`
echo "The user $s has logged more than 3 times"
fi
done
done
Please, check, how you thing, this script is correct ? Because, after output of my script in echo, i have that:
The user 4 21erdsf123sdfsdf
4 abcasbbabadab has logged more than 3 times
The user 4 21erdsf123sdfsdf
4 abcasbbabadab has logged more than 3 times
The user 4 21erdsf123sdfsdf
4 abcasbbabadab has logged more than 3 times
The user 4 21erdsf123sdfsdf
4 abcasbbabadab has logged more than 3 times
But i want to have output like, this:
The user 4 21erdsf123sdfsdf has logged more than 3 times
The user 4 abcasbbabadab has logged more than 3 times
And that's all. Where i have a mistake ? Please help.
You can do this in a very straightforward manner with
awk
This program scans the root.log creating an associative array (aka hash) with counts of every user, then prints the ones that are greater than one.
So, the only problem is that it's printing the same output multiple times? Why not just pipe through uniq? Dump to another temp file and then run uniq on it?
If that's not what you're asking about, I have some observations:
sort logfile | uniq -c
.UPDATE
Are you asking for advice in shell scripting or are you looking for a practical answer to the question of how do I find out who is currently logged into 3 or more sessions?
scripting advice:
$s
contains the whole string4 21erdsf123sdfsdf <newline> 4 abcasbbabadab
including a newline. I can't quite figure out why.awk '{print $5}'
? I tried copying your example data and running it throughawk '{print $5}'
and I got a bunch of newlines and nothing else.$temp_file
and$temp_file2
to make sure they're what you expect?tail -n 20
tells me you just want 'recent' entries and you don't care how recent. Is this true?cat /dev/null > $file
lines are redundant, just take them out.cat $logfile
withsort $logfile
Basically this script won't do what you want, and I can't tell how you intended it to work, so I can't give more specific advice. Sorry.
practical
who
command? EGwho | awk '{ print $1}' | sort | uniq -c | sort -d -r
or thelast
command?