Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

With help of the who command we can get active users, I want only the first field such as

user 1
user 2
user 3
share|improve this question
add comment

1 Answer 1

up vote 17 down vote accepted

You can get just the user names like so:

who | awk '{print $1}' | sort | uniq

Where who lists all logged in users, passes the output to awk which only prints the first section ("column") of text for every line, passes it to sort which sorts the output and passes it to uniq which removes duplicates.

share|improve this answer
5  
Or use sort -u instead of sort | uniq –  DK Bose Feb 26 at 11:00
    
Ok, did not know about that option. (Or knew and forgot!) –  Stabledog Feb 26 at 12:52
2  
You don't need to use awk: who | cut --delimiter=' ' --field=1 | sort --unique (the short version reads: who|cut -d' ' -f1|sort -u). –  Paddy Landau Mar 4 at 22:49
1  
Unix always provides so many ways to skin a cat. Its amazing any cats still have fur. –  Stabledog Mar 5 at 2:56
    
@PaddyLandau Why not put your answer in its own answer? –  AmanicA Mar 6 at 23:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.