gpt4 book ai didi

arrays - Shell Array Linux 中的唯一/无重复值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:36 26 4
gpt4 key购买 nike

我需要创建一个新数组或者只是从实际数组中删除重复的元素,

#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

因此我将获得具有重复 ips 的 ips_networks,但我只需要将每个 ip 中的一个放入另一个数组或相同的数组中,我尝试使用 awk,set -A(在我的 linux 上不起作用),cut 但没有运气,有没有办法制作一个独特的值(value)数组?

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com