PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > using uniq command for a string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

using uniq command for a string

Réponse
 
LinkBack Outils de la discussion
Vieux 10/12/2006, 12h17   #1
cconnell_1@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using uniq command for a string


I have 5 ip addresses and I have to make sure they are unique. They are
stored in variables ip1 ip2 ip3 ip4 and ip5

I want to run an operation that will return 5, i.e. they are unique.
Any other value will mean they are not unique.

If I put them in a string e.g.

"10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9"

And run uniq on them it doesnt work since uniq works on lines. How do I
get a similar operation to count unique occurences in a string?
Cheers

  Réponse avec citation
Vieux 10/12/2006, 12h49   #2
Bo Yang
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string

cconnell_1@lycos.com :
> I have 5 ip addresses and I have to make sure they are unique. They are
> stored in variables ip1 ip2 ip3 ip4 and ip5
>
> I want to run an operation that will return 5, i.e. they are unique.
> Any other value will mean they are not unique.
>
> If I put them in a string e.g.
>
> "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9"
>
> And run uniq on them it doesnt work since uniq works on lines. How do I
> get a similar operation to count unique occurences in a string?
> Cheers
>


You can first turn them into lines and then uniq.
echo "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9" | sed -n
's/ / \n/gp' | uniq
  Réponse avec citation
Vieux 10/12/2006, 13h22   #3
cconnell_1@lycos.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string


Bo Yang wrote:
> cconnell_1@lycos.com :
> > I have 5 ip addresses and I have to make sure they are unique. They are
> > stored in variables ip1 ip2 ip3 ip4 and ip5
> >
> > I want to run an operation that will return 5, i.e. they are unique.
> > Any other value will mean they are not unique.
> >
> > If I put them in a string e.g.
> >
> > "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9"
> >
> > And run uniq on them it doesnt work since uniq works on lines. How do I
> > get a similar operation to count unique occurences in a string?
> > Cheers
> >

>
> You can first turn them into lines and then uniq.
> echo "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9" | sed -n
> 's/ / \n/gp' | uniq


Cheers I will try that, I did try turning them into lines with sed,
with the \n but didnt put the /gp at the end of it. What does this do?

  Réponse avec citation
Vieux 10/12/2006, 13h39   #4
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string


<cconnell_1@lycos.com> wrote in message
news:1165753055.626092.197130@l12g2000cwl.googlegr oups.com...
>
> I have 5 ip addresses and I have to make sure they are unique. They are
> stored in variables ip1 ip2 ip3 ip4 and ip5
>
> I want to run an operation that will return 5, i.e. they are unique.
> Any other value will mean they are not unique.
>
> If I put them in a string e.g.
>
> "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9"
>
> And run uniq on them it doesnt work since uniq works on lines. How do I
> get a similar operation to count unique occurences in a string?


$ ip1=10.32.15.5
$ ip2=10.32.15.6
$ ip3=10.32.15.5

$ printf "%s\n" $ip{1..3}|sort -u
10.32.15.5
10.32.15.6

$ echo $ip{1..3} | gawk '!a[$0]++' RS="[ \n]"
10.32.15.5
10.32.15.6

Or:

$ printf "%s\n" 10.32.15.5 10.32.15.6 10.32.15.5|sort -u
10.32.15.5
10.32.15.6

$ printf "%s\n" 10.32.15.5 10.32.15.6 10.32.15.5|awk '!a[$0]++' RS="\n"
10.32.15.5
10.32.15.6


Regards
Dimitre




  Réponse avec citation
Vieux 10/12/2006, 14h24   #5
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string


"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
news:457c0dc3$0$49204$14726298@news.sunsite.dk...
>
> <cconnell_1@lycos.com> wrote in message
> news:1165753055.626092.197130@l12g2000cwl.googlegr oups.com...
>>
>> I have 5 ip addresses and I have to make sure they are unique. They are
>> stored in variables ip1 ip2 ip3 ip4 and ip5
>>
>> I want to run an operation that will return 5, i.e. they are unique.
>> Any other value will mean they are not unique.

[...]

I apologise,
I overlooked this detail. You can try something like this:

$ a=(10.32.15.1 10.32.15.2 10.32.15.3 10.32.15.4 10.32.15.5)
$ nums=($(printf "%s\n" ${a[@]}|sort -u))
$ [ "${#a[@]}" -eq "${#nums[@]}" ] && echo "${#a[@]}" || echo "They are NOT
unique"
5

$ a=(10.32.15.1 10.32.15.1 10.32.15.3 10.32.15.4 10.32.15.5)
$ nums=($(printf "%s\n" ${a[@]}|sort -u))
$ [ "${#a[@]}" -eq "${#nums[@]}" ] && echo "${#a[@]}" || echo "They are NOT
unique"
They are NOT unique


Regards
Dimitre


  Réponse avec citation
Vieux 10/12/2006, 15h22   #6
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string


"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
news:457c185c$0$49199$14726298@news.sunsite.dk...
>
> "Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
> news:457c0dc3$0$49204$14726298@news.sunsite.dk...
>>
>> <cconnell_1@lycos.com> wrote in message
>> news:1165753055.626092.197130@l12g2000cwl.googlegr oups.com...
>>>
>>> I have 5 ip addresses and I have to make sure they are unique. They are
>>> stored in variables ip1 ip2 ip3 ip4 and ip5
>>>
>>> I want to run an operation that will return 5, i.e. they are unique.
>>> Any other value will mean they are not unique.

> [...]


Or:

awk '{for(i=1;i<=NF;i++) a[$i]=1}
END{for(x in a)++c;if(c==NF)print c;else print "Not unique"}'



Regards
Dimitre


  Réponse avec citation
Vieux 10/12/2006, 18h13   #7
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string


cconnell_1@lycos.com wrote:
> I have 5 ip addresses and I have to make sure they are unique. They are
> stored in variables ip1 ip2 ip3 ip4 and ip5
>
> I want to run an operation that will return 5, i.e. they are unique.
> Any other value will mean they are not unique.
>
> If I put them in a string e.g.
>
> "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9"
>
> And run uniq on them it doesnt work since uniq works on lines. How do I
> get a similar operation to count unique occurences in a string?
> Cheers


ruby -e 'puts gets.split.uniq.size'

  Réponse avec citation
Vieux 10/12/2006, 21h48   #8
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using uniq command for a string

On 10 Dec 2006 05:22:39 -0800, cconnell_1@lycos.com
<cconnell_1@lycos.com> wrote:
>
> Bo Yang wrote:
>>
>> You can first turn them into lines and then uniq.
>> echo "10.32.15.5 10.32.15.6 10.32.15.7 10.32.15.8 10.32.15.9" | sed -n
>> 's/ / \n/gp' | uniq

>
> Cheers I will try that, I did try turning them into lines with sed,
> with the \n but didnt put the /gp at the end of it. What does this do?
>

The 'g' says to replace all spaces, not just the first, and 'p' says to
print the result (if you omit the -n, you don't need p).


--
SANTA CLAUS comes down a FIRE ESCAPE wearing bright blue LEG WARMERS
.... He scrubs the POPE with a mild soap or detergent for 15 minutes,
starring JANE FONDA!!
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h55.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15999 seconds with 16 queries