Tag Info

New answers tagged

0

$ cat need2sort pma2005 PMA2005 pma2005 $ LC_CTYPE="C" sort -fu need2sort pma2005 $ LC_CTYPE="C" sort -f need2sort | uniq PMA2005 pma2005


2

I assume that if there are duplicate entries there will always be one with ca as the second field. In your example data all the lines that have the same first field are grouped together, but you didn't mention whether that's always the case. If it is, then the task is a little simpler, but the awk script below will work even if the matching lines aren't ...


1

For an array with arbitrary values, it's quite tricky with bash as it doesn't have a builtin operator for that. bash however happens not to support storing NUL characters in its variables, so you can make use of that to pass that to other commands: The equivalent of zsh's: new_array=("${(@u}array}") on a recent GNU system, could be: eval "new_array=($( ...


0

To do it entirely in the shell and put the result in an array, declare -A seen for word in one two three two one do if [ ! "${seen[$word]}" ] then result+=("$word") seen[$word]=1 fi done echo "${result[@]}" In words: if we haven’t seen a given word yet, add it to the result array and flag it as ...


2

With GNU awk (this also retains original order) printf '%s\n' "1 2 3 2 1" | awk -v RS='[[:space:]]+' '!a[$0]++{printf "%s%s", $0, RT}' 1 2 3 To read into a bash array read -ra arr<<<$(printf '%s\n' "1 2 3 2 1" | awk -v RS='[[:space:]]+' '!a[$0]++{printf "%s%s", $0, RT}') printf "%s\n" "${arr[@]}" 1 2 3


1

If you are using zsh: $ array=(1 2 3 2 1) $ echo ${(u)array[@]} 1 2 3 or (if KSH_ARRAYS option is not set) even $ echo ${(u)array} 1 2 3


0

This solution worked for me. ids=(1 2 3 2 1) echo "${ids[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ' The above produces 1 2 3 as the output. Shorter version as suggested by Costas could be, printf "%s\n" "${ids[@]}" | sort -u | tr '\n' ' ' To store the end results to an array, you could do something like, IFS=$' ' arr=($(printf "%s\n" "${ids[@]}" | ...



Top 50 recent answers are included