LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: combine arrays & delete duplicates (https://www.linuxquestions.org/questions/programming-9/bash-combine-arrays-and-delete-duplicates-882286/)

jomann 05-23-2011 11:43 AM

Bash: combine arrays & delete duplicates
 
Hi all,

I would like to combine two arrays and delete duplicates that might occur.
I tried to solve it like this:
Code:

combined=( "${results1[@]}" "${results2[@]}" )
printf "${combined[@]}" | sort -n | uniq

Example:

result1 (4 elements):
newfoo
new foo
oldfoo
new

result2 (4 elements):
new foo
foo
foo new
new

combine (6 elements only):
newfoo
new foo
oldfoo
foo
foo new
new

In my code printf seems to have a problem with elements that have the same letters but a space inbetween. For instance "new foo", "newfoo" are the same for printf :-(

Anybody can help, plz?

Regards & thanks, jomann.

jomann 05-23-2011 01:03 PM

Now I did the following:

Code:

# combine the two arrays to one without duplicate elements:
tmp_a=/tmp/tmpfile
combined=( "${results1[@]}" "${results2[@]}" )
true > $tmp_a

for i in ${combined[@]}; do
        echo -en "$i\n" >> $tmp_a
done

# get rid of duplicates in array:
unique=`sort -n $tmp_a | uniq`
echo -en "Results without duplicates: \n$unique\n"

This works. I do not really like it. I would prefer not to write a tmp-file, but to manipulate the combined array itself.

*Or does anybody have a better idea?*

Regards, jomann.

Nominal Animal 05-23-2011 01:36 PM

Code:

OLDIFS="$IFS"
IFS=$'\n'
combined=(`for R in "${results1[@]}" "${results2[@]}" ; do echo "$R" ; done | sort -du`)
IFS="$OLDIFS"


jomann 05-23-2011 01:47 PM

Yes, that's much better!
Thanks Nominal Animal.

Regards, jomann.

everToulouse 05-23-2011 02:34 PM

Hi,

that's because you don't use printf the right way : in printf, f stands for format, and you don't give one, so
Code:

result1=( newfoo 'new foo' oldfoo new )
result2=( 'new foo' foo 'foo new' )
combined=( "${result1[@]}" "${result2[@]}" )
printf '%s\n' "${combined[@]}" | sort -u
foo
foo new
new
newfoo
new foo
oldfoo

six element!

jomann 05-24-2011 05:42 AM

Thanks again for the additional clearing of my printf-phenomenon.

Regards,
jomann.


All times are GMT -5. The time now is 03:21 PM.