The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
uniq command??? skyineyes UNIX for Dummies Questions & Answers 2 05-28-2008 06:27 AM
using uniq and awk?? amatuer_lee_3 Shell Programming and Scripting 13 05-17-2008 01:10 PM
Compare 2 files and give uniq output rauphelhunter Shell Programming and Scripting 1 05-12-2008 04:47 PM
compare two col from 2 files, and output uniq from file 1 pp56825 Shell Programming and Scripting 2 01-10-2008 11:10 AM
help on UniQ vishal_ranjan HP-UX 0 06-21-2007 10:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2008
lochraven lochraven is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 29
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 (permalink)  
Old 10-24-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool 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 04:17 PM.. Reason: added some explanation
  #3 (permalink)  
Old 10-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Or:

Code:
who | awk '!($1 in a){a[$1];print $1}'
Regards
  #4 (permalink)  
Old 10-24-2008
lochraven lochraven is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 29
thats awesome, thanks to the both of you

How about excluding 'root' from the output?
  #5 (permalink)  
Old 10-24-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
who | awk '!($1 in a) && !/root/{a[$1];print $1}'
  #6 (permalink)  
Old 10-24-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool

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]
}'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0