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

前端 未结 14 1166
-上瘾入骨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条回答
  •  萌比男神i
    2020-11-27 13:31

    # Read a file into variable
    lines=$(cat /path/to/my/file)
    
    # Go through each line the file put in the variable, and assign it a variable called $line
    for line in $lines; do
      # Print the line
      echo $line
    # End the loop, then sort it (add -u to have unique lines)
    done | sort -u
    

提交回复
热议问题