comparing output of who command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing output of who command
# 1  
Old 02-11-2011
comparing output of who command

I have a need to take the output of the who command and first strip out users with only 1 session, then of the remaining users I need only to list the ones who are using more than 1 IP address

Normal output:

Code:
tander01    pts/113     Feb 11 09:21     (10.25.1.23)    
tbanks      pts/81      Feb 11 08:19     (10.2.1.20)     
tbucy       pts/28      Feb 11 07:11     (10.14.1.11)    
tchhuom     pts/100     Feb 11 08:45     (10.10.1.16)    
tchhuom     pts/101     Feb 11 08:45     (10.10.1.16)    
tchhuom     pts/182     Feb 11 13:55     (10.10.1.30)    
tcowan      pts/109     Feb 11 09:01     (10.201.33.1)   
tdavis04    pts/128     Feb 11 13:07     (10.10.1.47)

Expected result:


Code:
 
tchhuom     pts/100     Feb 11 08:45     (10.10.1.16)    
tchhuom     pts/101     Feb 11 08:45     (10.10.1.16)    
tchhuom     pts/182     Feb 11 13:55     (10.10.1.30)


I would appreciate any advice/help on this.
# 2  
Old 02-11-2011
Something like...

I copied the output of your who command to a sample file. So in place of cat sample7.txt you would substitute your who command.

Code:
$ cat sample7.txt
tander01    pts/113     Feb 11 09:21     (10.25.1.23)
tbanks      pts/81      Feb 11 08:19     (10.2.1.20)
tbucy       pts/28      Feb 11 07:11     (10.14.1.11)
tchhuom     pts/100     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/101     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/182     Feb 11 13:55     (10.10.1.30)
tcowan      pts/109     Feb 11 09:01     (10.201.33.1)
tdavis04    pts/128     Feb 11 13:07     (10.10.1.47)

$ grep `cat sample7.txt | cut -d" " -f1 | sort | uniq -d` sample7.txt
tchhuom     pts/100     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/101     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/182     Feb 11 13:55     (10.10.1.30)


$ cat sample7.txt | grep `cat sample7.txt | cut -d" " -f1 | sort | uniq -d`
tchhuom     pts/100     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/101     Feb 11 08:45     (10.10.1.16)
tchhuom     pts/182     Feb 11 13:55     (10.10.1.30)

This User Gave Thanks to joeyg For This Post:
# 3  
Old 02-11-2011
Thanks for the fast reply, however I run into issues. I even tried sending the output of who to a file first, but my results are not the same. This is on AIX 6.1 if that matters.

Code:
$ who >> who.out

$ cat who.out | grep `cat who.out | cut -d" " -f1 | sort | uniq -d`
grep: can't open aparso01
grep: can't open apeter10
grep: can't open astir
grep: can't open bsugar
grep: can't open cparker
grep: can't open cscott

---------- Post updated at 03:09 PM ---------- Previous update was at 02:53 PM ----------

This works well in linux :shrug:

---------- Post updated at 04:03 PM ---------- Previous update was at 03:09 PM ----------

this part works great

cat who.out | cut -d" " -f1 | sort | uniq -d


but its when I add the who|grep is when it complains.

---------- Post updated at 04:20 PM ---------- Previous update was at 04:03 PM ----------

Here is what worked for me. You definitely got me on the right track


Code:
for username in $(who |cut -d " " -f1 |sort |uniq -d)
do
    who |grep $username >>$OUTPUTFILE

done

# 4  
Old 02-12-2011
Above code can only be used to find the username which have multi-login session.

Not for finding the users who login with different IPs.

For example, if tchhuom login with same IPs several times, you still got this name.

Try this:
Code:
awk 'NR==FNR {if (!a[$1]) {a[$1]=$NF} else {if (a[$1]!=$NF) b[$1]};next} $1 in b ' infile infile

# 5  
Old 02-12-2011
wow thats great. I really should learn awk.


I used my above code but continued on and ended up with the same output, just much more work

Code:
for username in $(who |cut -d " " -f1 |sort |uniq -d)
do
   who |grep $username >>$TEMP

done

#GET LIST OF UNIQUE IP's FOR USERS WITH MULTIPLE SESSIONS

cat $TEMP|cut -b 42-180|sort |uniq -u >>$IPS


#USE THE LIST OF IP'S TO GENERATE A USER LIST

for ips in $(cat $IPS)
do
   who |grep $ips |cut -d " " -f1 >>$OUTPUT2
done

#USING THE USER LIST, CREATE OUTPUT FILE

for last in $(cat $OUTPUT2)
do
  who |grep $last >>$OUTPUT
done

I realize its ugly now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Homework & Coursework Questions

Getting a command not found when comparing in tsch

Hey everyone, I am almost done with this assignment I have to do but there is one last thing nagging me that I can't seem to find. In my script I am feeding line by line to a ./test file and I am getting the return answer correctly. The goal is to test the current script (check.sh) and if it's... (1 Reply)
Discussion started by: tastybrownies
1 Replies

3. Shell Programming and Scripting

Comparing 2 xml files and print the differences only in output

Hi....I'm having 2 xml files, one is having some special characters and another is a clean xml file does not have any special characters. Now I need one audit kind of file which will show me only from which line the special characters have been removed and the special characters. Can you please... (1 Reply)
Discussion started by: Krishanu Saha
1 Replies

4. Shell Programming and Scripting

Comparing text in 2 files and output difference in another file.

I have 2 files of almost same text apart from 2,3 ending lines. Now I want to get that difference in another file. e.g file1.txt is Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_livecd-lv_root 18G 2.4G 15G 14% / tmpfs 504M ... (12 Replies)
Discussion started by: kashif.live
12 Replies

5. Shell Programming and Scripting

Comparing a text file's entries with ps -ef output

Shell : Korn os : AIX This is the ps output looking for a process called pmon. pmon runs with various 'service' names which is appended with an underscore as shown below. For example a pmon process for the service hexjkm will be named ora_pmon_hexjkm $ ps -ef | grep pmon oracle... (1 Reply)
Discussion started by: kraljic
1 Replies

6. Shell Programming and Scripting

Comparing a command with a string

Is this possible? If so it seems like a very easy and short script.I'd like to make a script that will print a friendly message but only if it is on your personal terminal. so the code would look like this: #!/bin/csh if ; then echo "This is a friendly message :D "; fi thats what i have... (4 Replies)
Discussion started by: Waffles
4 Replies

7. Shell Programming and Scripting

Comparing file with Sql output.

Hi Guys, I need to compare the sql output with a column present in a file. I am able to read the column from the file and stored in a variable. Can somebody help how to store output of the below query to a variable. The database is Oracle. Select count(*) from tableName; ... (2 Replies)
Discussion started by: mac4rfree
2 Replies

8. Shell Programming and Scripting

comparing in files and output

i have a file a with contents tom lasole jon gille sam and a file b with contents tom|1234|abcf|newyork,ohio,oregon sam|2345|drft|texas,london hyle|4444|befr|wisconsin neyo|2333|tdtt|ohio,jersey i want to compare records in file a to file b such that when the name tom is... (5 Replies)
Discussion started by: dealerso
5 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. Shell Programming and Scripting

Find duplicate value comparing 2 files and create an output

I need a perl script which will create an output file after comparing two diff file in a directory path: /export/home/abc/file1 /export/home/abc/file2 File Format: <IP>TAB<DeviceName><TAB>DESCRIPTIONS file1: 10.1.2.1.3<tab>abc123def<tab>xyz.mm1.ppp.... (2 Replies)
Discussion started by: ricky007
2 Replies
Login or Register to Ask a Question