gpt4 book ai didi

linux - 通过 Unix shell 脚本查找和删除重复的字符串。如何?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:14 25 4
gpt4 key购买 nike

我是 Unix shell 脚本新手。我知道几种不同的查找重复项的方法。但是找不到一种简单的方法来删除重复项同时保持原始顺序(因为使用 sort -u 会丢失原始顺序)。

示例:名为 dedupe.sh 的脚本

样本运行:

dedupe.sh

cat dog cat bird fish bear dog

结果为:cat dog bird fish bear

最佳答案

使用 :

$ printf '%s\n' cat dog cat bird fish bear dog | awk '!arr[$1]++'
cat
dog
bird
fish
bear

$ echo 'cat dog cat bird fish bear dog' | awk '!arr[$1]++' RS=" "

$ printf '%s\n' cat dog cat bird fish bear dog | sort -u

如果它在 中工作,它将在脚本中工作 =)

关于linux - 通过 Unix shell 脚本查找和删除重复的字符串。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15468124/

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