我需要创建一个新数组,或者只是从实际数组中删除重复元素,
#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
所以我会得到具有重复ip的ips_networks,但我只需要将每个ip中的一个放入另一个数组或相同的数组中,我尝试使用awk,设置-A(在我的linux上不起作用),剪切但没有运气好,有没有办法制作一个唯一值数组?
最佳答案
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
关于arrays - Shell Array Linux 中的唯一/无重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798299/