使用awk
和process-substitution
只能比使用sort
和其他工具。
awk -vORS="," '!seen[$1]++' < <(echo "[email protected],[email protected],[email protected],[email protected],[email protected]" | tr ',' '\n')
[email protected],[email protected],[email protected]
或者另一種方式來使用純bash和避免tr
完全是
# Read into a bash array with field-separator as ',' read with '-a' for reading to an array
IFS=',' read -ra myArray <<< "[email protected],[email protected],[email protected],[email protected],[email protected]"
# Printing the array elements new line and feeding it to awk
awk -vORS="," '!seen[$1]++' < <(printf '%s\n' "${myArray[@]}")
[email protected],[email protected],[email protected]