LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2008, 05:24 AM   #1
qnxqnx
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
How to exclude repetitions of words in the shell output?


Hi,

I have a line with N words as a shell output. How I can exclude those words that appear more than once?

This is what I do. I need to print the list of current users on the system.

# who -q | grep -v '#'

Gives me the string of users:

mike mike john mike john mike mike mike

The usernames are repeating (each user logged in several times), which I donít need. I need just this:

mike john

How I can exclude all the repetitions in the output? grep, sed, awk?

Thanks!!!
 
Old 07-30-2008, 06:13 AM   #2
Uxinn
Member
 
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47

Rep: Reputation: 16
this works, but maybe there is a easier way about this.

Code:
#!/bin/bash
UNIQUE=
for name in `who|awk '{print $1}'|sort -d`;do
        if [ "X$UNIQUE" != "X$name" ];then
                echo $name
        fi
UNIQUE=$name
done
 
Old 07-30-2008, 06:41 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
who|awk '{print $1}'|sort -u
 
Old 07-30-2008, 06:43 AM   #4
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
Code:
who -u | cut -d' ' -f1 | sort | uniq
 
Old 07-30-2008, 07:05 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
who -q | grep -v '#' | tr " " "\n"| sort -u
 
Old 07-30-2008, 07:15 AM   #6
Uxinn
Member
 
Registered: May 2008
Location: Iceland
Distribution: Ubuntu Hardy
Posts: 47

Rep: Reputation: 16
Quote:
Originally Posted by chrism01 View Post
who|awk '{print $1}'|sort -u
Ahh forgot about sort -u, good one
 
Old 07-30-2008, 07:22 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A little off topic but... don't forget
Code:
who mom likes
 
Old 07-30-2008, 07:57 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Alternately

who|awk '{print $1}'|uniq
 
Old 07-30-2008, 08:59 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Another way:
Code:
who | awk '{array[$1]} END{for (i in array) print i}'
 
Old 07-30-2008, 11:31 AM   #10
ne pas
Member
 
Registered: Jul 2008
Posts: 55

Rep: Reputation: 23
Quote:
Originally Posted by chrism01 View Post
Alternately

who|awk '{print $1}'|uniq
You need to pre-sort the input for uniq, so duplicate lines are adjacent to one another, otherwise uniq will not treat them as duplicates.
 
Old 07-30-2008, 12:43 PM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by colucix View Post
A little off topic but... don't forget
Code:
who mom likes
Offtopic:

These funnies were always great. The two variants below and their (usually) identical output often befuddled new users:

$ who am i?
mrc ttyp0 Jul 25 20:23 (mrc.example.com)

$ who am i
mrc ttyp0 Jul 25 20:23 (mrc.example.com)

And of the omniscience of csh:
% Got a light?
Got: No match.

Ah, those were the days... :-)
 
Old 07-30-2008, 01:08 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by Mr. C. View Post
Ah, those were the days... :-)
Uh yeah... and don't forget the good ol'...

# make love
Make: Don’t know how to make love. Stop.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command(s) comparing 3 files and changing capitalization of words (+output) kuser:) Linux - General 4 03-21-2012 08:37 AM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 01:57 PM
Keystroke repetitions in the login screen rendering it unusable. sidholland Mandriva 8 07-15-2007 05:15 PM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
How can I filter the output of grep to exclude certain cases? QtCoder Linux - General 1 03-28-2004 12:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration