LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to exclude repetitions of words in the shell output? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-exclude-repetitions-of-words-in-the-shell-output-659217/)

qnxqnx 07-30-2008 05:24 AM

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!!!

Uxinn 07-30-2008 06:13 AM

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


chrism01 07-30-2008 06:41 AM

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

ne pas 07-30-2008 06:43 AM

Code:

who -u | cut -d' ' -f1 | sort | uniq

ghostdog74 07-30-2008 07:05 AM

Code:

who -q | grep -v '#' | tr " " "\n"| sort -u

Uxinn 07-30-2008 07:15 AM

Quote:

Originally Posted by chrism01 (Post 3230831)
who|awk '{print $1}'|sort -u

Ahh forgot about sort -u, good one :cool:

colucix 07-30-2008 07:22 AM

A little off topic but... don't forget
Code:

who mom likes
;)

chrism01 07-30-2008 07:57 AM

Alternately

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

colucix 07-30-2008 08:59 AM

Another way:
Code:

who | awk '{array[$1]} END{for (i in array) print i}'

ne pas 07-30-2008 11:31 AM

Quote:

Originally Posted by chrism01 (Post 3230904)
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.

Mr. C. 07-30-2008 12:43 PM

Quote:

Originally Posted by colucix (Post 3230872)
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... :-)

colucix 07-30-2008 01:08 PM

Quote:

Originally Posted by Mr. C. (Post 3231196)
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.

;)


All times are GMT -5. The time now is 06:03 AM.