|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
<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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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' |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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!! |
|
![]() |
| Outils de la discussion | |
|
|