LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-23-2011, 12:43 PM   #1
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Rep: Reputation: 0
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.
 
Old 05-23-2011, 02:03 PM   #2
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
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.
 
Old 05-23-2011, 02:36 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
Code:
OLDIFS="$IFS"
IFS=$'\n'
combined=(`for R in "${results1[@]}" "${results2[@]}" ; do echo "$R" ; done | sort -du`)
IFS="$OLDIFS"
 
Old 05-23-2011, 02:47 PM   #4
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
Yes, that's much better!
Thanks Nominal Animal.

Regards, jomann.
 
Old 05-23-2011, 03:34 PM   #5
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
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!
 
Old 05-24-2011, 06:42 AM   #6
jomann
LQ Newbie
 
Registered: Mar 2011
Posts: 24

Original Poster
Rep: Reputation: 0
Thanks again for the additional clearing of my printf-phenomenon.

Regards,
jomann.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Package to find and delete duplicates maury0324 Linux - Software 8 08-03-2010 06:47 AM
combine bash & expect with variable from user input ndnd Linux - Newbie 2 09-17-2009 10:18 AM
how could I delete duplicates entries in xml using php catzilla Programming 2 10-30-2005 08:08 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 03:57 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM


All times are GMT -5. The time now is 09:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration