How can I get unique values from an array in Bash?

前端 未结 14 1162
-上瘾入骨i
-上瘾入骨i 2020-11-27 12:50

I\'ve got almost the same question as here.

I have an array which contains aa ab aa ac aa ad, etc. Now I want to select all unique elements from this ar

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 13:27

    cat number.txt

    1 2 3 4 4 3 2 5 6
    

    print line into column: cat number.txt | awk '{for(i=1;i<=NF;i++) print $i}'

    1
    2
    3
    4
    4
    3
    2
    5
    6
    

    find the duplicate records: cat number.txt | awk '{for(i=1;i<=NF;i++) print $i}' |awk 'x[$0]++'

    4
    3
    2
    

    Replace duplicate records: cat number.txt | awk '{for(i=1;i<=NF;i++) print $i}' |awk '!x[$0]++'

    1
    2
    3
    4
    5
    6
    

    Find only Uniq records: cat number.txt | awk '{for(i=1;i<=NF;i++) print $i|"sort|uniq -u"}

    1
    5
    6
    

提交回复
热议问题