conorao
conorao

Reputation: 3

Find duplicate items in string

I've got a big string of IP addresses in a bash script stored as a variable.
My question is: How can I verify that all of the IP's in the string appear the same number of times? If this helps at all, the number of times that all of the IP's should occur in the string is also stored as a variable.

Here's an example to show you what I mean:

String "4.2.2.2 8.8.8.8 4.2.2.2 4.2.2.2 8.8.8.8 8.8.8.8" 

would be OK

String "1.1.1.1 2.2.2.2 1.1.1.1 2.2.2.2 2.2.2.2"

would not be OK because there are 3 instances of 2.2.2.2 and two of 1.1.1.1

String "4.4.4.4 3.3.3.3 2.2.2.2"

would be OK because they're all in there once.

Upvotes: 0

Views: 5140

Answers (3)

beny23
beny23

Reputation: 35068

You could use:

$ STR="4.2.2.2 8.8.8.8 4.2.2.2 4.2.2.2 8.8.8.8 8.8.8.8"
$ echo $STR | tr " " "\n" | sort | uniq -c | grep -v " 1 "
   3 4.2.2.2
   3 8.8.8.8
$ STR="4.4.4.4 3.3.3.3 2.2.2.2"
$ echo $STR | tr " " "\n" | sort | uniq -c | grep -v " 1 "
$

(no output => OK, with output => Not OK).

or

$ STR="4.2.2.2 8.8.8.8 4.2.2.2 4.2.2.2 8.8.8.8 8.8.8.8"
$ ISOK=`echo $STR | tr " " "\n" | sort | uniq -c | grep -v " 1 "`
$ if [[ -z $ISOK ]]; then echo "Is OK"; fi
$ 

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363818

s="4.2.2.2 8.8.8.8 4.2.2.2 4.2.2.2 8.8.8.8 8.8.8.8"
n=`echo $s | tr " " "\n" | wc -l`
nuniq=`echo $s | tr " " "\n" | sort | uniq | wc -l`
[ $n -eq $nuniq ] || echo "we've got duplicates"

or

echo $s | tr " " "\n" | sort | uniq -c | grep -qv '^ *1 ' && echo "duplicates!"

Upvotes: 4

asf107
asf107

Reputation: 1146

Here's a simple way to do this with bash and awk:

(for ip in $string; do echo $ip; done) | sort | uniq -c | awk 'BEGIN{badString=0}; $1!=1{badString=1} END{if(badString == 1) { print "This was a bad string!"}}'

In words: split each IP in the string into distinct lines, sort them, check to see if the list is unique by counting the occurrences of each IP address, then print "This was a bad string" if any count is not one.

I only did it this way because you tagged with "bash" and "shell," but there exist much simpler perl one-liners to do this :)

Upvotes: 0

Related Questions