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 Code:
sort firstFILE secondFILE | uniq > masterFILE The closest I've come (not close at all) is Code:
masterLIST=${firstLIST[@]}" "${secondLIST[@]} Code:
$echo ${masterLIST[@]} Thanks for any help, Chris |
Here's one solution (OK for the simple numbers in the example, would need tweaking if the lines contained whitespace):
Code:
#!/bin/bash |
Another alternative:
Code:
#!/bin/bash |
And another slight change I thought of on the way home :)
Code:
#!/bin/bash |
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[@]} Thanks again, Chris |
All times are GMT -5. The time now is 09:19 AM. |