2

I need to make a new array or just delete from the actual array the duplicate elements,

#The NTP IPS are the following ones:
#10.30.10.0, 10.30.10.0, 10.30.20.0, 10.30.20.0, 10.30.20.0

#!/bin/bash
ips_networks=()
for ip in ${ips_for_ntp[@]};do
    ips_networks+=${ip%.*}.0
done

So I'll get ips_networks with duplicate ips, but I need just one of each ip into another array or the same, I have try with awk, set -A (Is not working on my linux), cut but with no luck, is there anyway to make an unique value array?

1
  • It is declare -A for an associative array not set -A and it needs bash 4+. Commented Apr 22, 2015 at 13:04

1 Answer 1

2
ips="10.30.10.0, 10.30.10.0, 10.30.20.0, 10.30.20.0, 10.30.20.0"
unique_ips=`echo $ips | sed -e "s/\s\\+//g" | sed -e "s/,/\\n/g"| sort | uniq`
echo $unique_ips #10.30.10.0 10.30.20.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works but using the comma to separate, the thing is this ips are already in the array "ips_networks" so if make a echo it will be like this: 10.30.10.010.30.10.010.30.20.010.30.20.010.30.20.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.