LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2010, 12:48 AM   #1
cmbouchard
LQ Newbie
 
Registered: Nov 2010
Location: Urbana, IL
Posts: 2

Rep: Reputation: 0
bash - merging strings (perhaps with sort | uniq)


Hi everyone,

I've used this site for a while an unregistered user. This is my first post.

I have multiple strings (eg. say two, firstLIST=(0 1 2) and secondLIST=(2 3)) and want to create a single string composed of their unique sorted elements. For the sample strings above, I'd like to build masterLIST=(0 1 2 3).

I suppose I could write the elements of firstLIST and secondLIST to files

Code:
echo ${firstLIST[@]} > firstFILE
echo ${secondLIST[@]} > secondFILE
then use

Code:
sort firstFILE secondFILE | uniq > masterFILE
as this gives me a file populated with the elements I'm after, but I'm not sure how to read the elements back into masterLIST... and it doesn't seem "right" to create files to accomplish this. Is there a way to do this by manipulating the strings ${firstLIST[@]} and ${secondLIST[@]} directly?

The closest I've come (not close at all) is

Code:
masterLIST=${firstLIST[@]}" "${secondLIST[@]}
but masterLIST built this way has only one element

Code:
$echo ${masterLIST[@]}
0 1 2 2 3
$echo ${#masterLIST[@]}
1
and I don't have access to the individual digits to then try to figure out how to remove duplicates.

Thanks for any help,
Chris

Last edited by cmbouchard; 11-16-2010 at 12:54 AM.
 
Old 11-16-2010, 01:10 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Here's one solution (OK for the simple numbers in the example, would need tweaking if the lines contained whitespace):
Code:
#!/bin/bash

# Load arrays
firstLIST=(0 1 2)
secondLIST=(2 3)
masterLIST=( ${firstLIST[*]} ${secondLIST[*]} )

# Generate results array
while read
do
    newLIST+=( $REPLY )
done <<< $(
        for (( i=0; i < ${#masterLIST[*]}; i++ ))
        do
            echo "${masterLIST[i]}"
        done | sort | uniq
    )

# Show results
for (( i=0; i < ${#newLIST[*]}; i++ ))
do
    echo "$i: ${newLIST[i]}"
done
 
Old 11-16-2010, 01:40 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Another alternative:
Code:
#!/bin/bash

# Load arrays
firstLIST=(0 1 2)
secondLIST=(2 3)
tmpLIST=( ${firstLIST[*]} ${secondLIST[*]} )

for x in ${tmpLIST[*]}
do
    for (( i=0; i< ${#newLIST[*]}; i++))
    do
        (( x == ${newLIST[i]} )) && continue 2
    done

    newLIST+=($x)
done

echo "${newLIST[*]}"
 
Old 11-16-2010, 04:37 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And another slight change I thought of on the way home
Code:
#!/bin/bash

# Load arrays
firstLIST=(0 1 2)
secondLIST=(2 3)
tmpLIST=( ${firstLIST[*]} ${secondLIST[*]} )

for x in ${tmpLIST[*]}
do
    [[ ${newLIST[*]} =~ " $x " ]] || newLIST+=($x)
done

echo "${newLIST[*]}"
 
Old 11-16-2010, 11:21 PM   #5
cmbouchard
LQ Newbie
 
Registered: Nov 2010
Location: Urbana, IL
Posts: 2

Original Poster
Rep: Reputation: 0
solutions for sorting unique elements of bash arrays

grail and catkin,

Thanks for your solutions! I'd cobbled together the following inferior solution of writing out to files, working with the files, then reading into an array,

Code:
count1=${#firstLIST[@]}
count2=${#secondLIST[@]}

for (( i=0; i<$count1; i++ )); do
  echo ${firstLIST[$i]} >> tmp_file
done
for (( i=0; i<$count2; i++ )); do
  echo ${secondLIST[$i]} >> tmp_file
done

sort -u tmp_file > tmp_file
masterLIST=( `cat tmp_file` )
but much prefer how you have worked directly with the arrays.

Thanks again,
Chris
 
  


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
CSV file - Duplicate records need merging | BASH? lmedland Programming 21 12-10-2010 05:01 AM
Returning Strings - Bash isdigit Linux - Newbie 6 12-24-2009 11:52 PM
sort & uniq tostay2003 Programming 3 06-28-2008 06:14 PM
Need help with bash and strings Histamine Programming 4 06-27-2007 04:18 PM
bash and strings graziano1968 Linux - Software 2 10-01-2004 06:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:37 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
Open Source Consulting | Domain Registration