who - uniq output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting who - uniq output
# 1  
Old 10-24-2008
who - uniq output

Hi,
I'd like to have a script what takes the 'who' output and grabs the user names and outputs just the user name, and no duplicates.

I know I could do something like:

who | awk '{print $1}' | uniq -u

but I'd like to stay away from using the 'uniq' comand and just use awk.

Thanks
# 2  
Old 10-24-2008
Tools Perhaps someone might clean this up a bit

The following works, but not sure if all necessary.

Code:
#! /usr/bin/bash

who | awk '{line[$1] = $1
     sum[$1]++}
    END{
     for(i in sum)
     if (sum[i]>0)
       print line[i]
}'

set array variable to the first read (which is name) field
increment a counter so I can know if anything in array value

step thru all values in my sum array
if the value in sum > 0 (meaning I have a match)
print out the corresponding entry in the line array

Last edited by joeyg; 10-24-2008 at 05:17 PM.. Reason: added some explanation
# 3  
Old 10-24-2008
Or:

Code:
who | awk '!($1 in a){a[$1];print $1}'

Regards
# 4  
Old 10-24-2008
thats awesome, thanks to the both of you Smilie

How about excluding 'root' from the output?
# 5  
Old 10-24-2008
Code:
who | awk '!($1 in a) && !/root/{a[$1];print $1}'

# 6  
Old 10-24-2008
Tools

Excluding root

Code:
> cat uniq_names
#! /usr/bin/bash

who | awk '$1!~"root" {line[$1] = $1
     sum[$1]++}
    END{
     for(i in sum)
     if (sum[i]>0)
       print line[i]
}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uniq help

hello I want to check on first column duplicates and print the unique first and second columns My trial output is not generating what I needed, i.e the second column. thanks in advance (5 Replies)
Discussion started by: bhargavpbk88
5 Replies

2. Shell Programming and Scripting

Uniq not doing what I want it to

I have a master list of servers. I also have a list of servers I'm not supposed to touch. I'm trying to filter out the list servers that I'm not supposed to touch from the master list of servers, so I will have a "master list of servers I can touch". When I try to filter these I'm not getting... (4 Replies)
Discussion started by: MaindotC
4 Replies

3. UNIX for Dummies Questions & Answers

HELP! showing output as a ratio in uniq

Hi, I have the following file called addresses, (it is a large file i have only copy and pasted few of the data below) and I am wanting to write a command so it will Find the ratio of mobile (07....) to land line (01....) telephone numbers? then find the most popular first name and list the... (1 Reply)
Discussion started by: tina_2010
1 Replies

4. Shell Programming and Scripting

uniq -c

When I do uniq -c on a list of sorted numbers, for eg: 1 1 2 2 2 3 3 4 It outputs 2 1 3 2 2 3 1 4. Now, is there a way to sort on the column that "uniq -c" produced? (2 Replies)
Discussion started by: prasanna1157
2 Replies

5. UNIX for Dummies Questions & Answers

| Help | unix | grep | sort | uniq - Different output from what I thought would be the same

Hello, I'm having an consistency issue.... grep 'a' /usr/share/dict/words 1) This will highlight every 'a' in each word. grep 'a\{1,\}' /usr/share/dict/words 2) This will highlight 'a' if it occurs at least once in a sequence. So every 'a'. Output of 1) I would... (1 Reply)
Discussion started by: MykC
1 Replies

6. UNIX for Dummies Questions & Answers

Difference between plain "uniq" and "uniq -u"

Dear all, It's not entirely clear to me from manpage the difference between them. Why we still need "-u" flag? - monkfan (3 Replies)
Discussion started by: monkfan
3 Replies

7. Shell Programming and Scripting

Compare 2 files and give uniq output

Hi , Just to find out a way to compare these 2 files and give unique output. For eg: 1.txt contains 1 2 3 4 5 6 -------------------------------------- 2.txt contains 1 2 6 8 (1 Reply)
Discussion started by: rauphelhunter
1 Replies

8. Shell Programming and Scripting

uniq options

Hi I am currently using uniq -u and uniq -d option in my program to get uniq and duplicate lines from file. What i doing is uniq -c file1>file_u uniq -d file1>file_d cat file_u file_d > file_fiinal Since i am procesing a larger files the I/O operations is costly affair. Hence I would... (0 Replies)
Discussion started by: dhanamurthy
0 Replies

9. Shell Programming and Scripting

compare two col from 2 files, and output uniq from file 1

Hi, I can't find how to achive such thing, please help. I have try with uniq and comm but those command can't compare columns just whole lines, I think awk will be the best but awk is magic for me as of now. file a a1~a2~a3~a4~a6~a7~a8 file b b1~b2~b3~b4~b6~b7~b8 output 1: compare... (2 Replies)
Discussion started by: pp56825
2 Replies

10. HP-UX

help on UniQ

All, Can anybody provide me the links to the documentation on UniQPrint? I need to prepare some documents to help my co-workers to learn UniQPrint. Regards, Vishal (0 Replies)
Discussion started by: vishal_ranjan
0 Replies
Login or Register to Ask a Question