Compare two arrays by values [BASH]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two arrays by values [BASH]
# 1  
Old 08-26-2014
Compare two arrays by values [BASH]

I have 2 arrays of values for example

Code:
A1 ={10 15 3 21}
A2 ={10 15 3 22}

I need to check which one has greater values. However:

Code:
A1 ={10 15 3 21}
A2 ={10 15 3 21 3} - this one would be greater.

A1 ={10 15 5 21} - this one greater
A2 ={10 15 3 21}

Basically, I want to compare patch numbers. What would be the fastest way to do that?

Thanks in advance.

Last edited by Jutsimitsu; 08-26-2014 at 07:01 AM..
# 2  
Old 08-26-2014
Hello Jutsimitsu,

Welcome to the forum, could you please see the option code tags while posting any post, as per forum rules which you agreed during the creation of your user account. Please put all kind of commands and cods into code tags. You can also use Preview post option to see how post looks like before posting it. Hope this helps you.

Also kindly let us know the All in All Input as well as required Output for same, it will be helpful for us to guide you.



Thanks,
R. Singh
# 3  
Old 08-26-2014
I'm afraid you can't escape comparing the arrays element by element, in e.g. a for loop. If you show us what you tried and where you got stuck, we'll be glad to help.
# 4  
Old 08-26-2014
Thanks for response, edited first post with code tags.

What I want it to return is an array, either A1 or A2. As for input, it's just those two arrays. I mean it was in strings of 10.15.5.21 for example but I split it with IFS.

I thought as much as to compare those arrays in loop. I admit I am a C++ programmer, so bash scripting is quite uncommon for me. I got stuck quite early, with loop like:

Code:
for i in "${A1[@]}"
do
    if [ ${#A1[@]} -eq ${#A2[@]} ]; then
        echo "TRUE"
    else 
        echo "FALSE"
    fi
done

I don't know how to get an iterator for both arrays. Then check if array value is not null, then how to break if I find out which one is greater.
# 5  
Old 08-26-2014
One way might be something like:
Code:
for ((i=0; i<${#A1[@]} || i<${#A2[@]}; i++))
do
  if (( ${A1[i]:-0} > ${A2[i]:-0} )); then
    echo A1
    break
  elif (( ${A1[i]:-0} < ${A2[i]:-0} )); then
    echo A2
    break
  fi
done


Last edited by Scrutinizer; 08-26-2014 at 08:01 AM..
# 6  
Old 08-26-2014
Try
Code:
echo "(" ${A1[@]} "), (" ${A2[@]} ")"
( 10 15 3 21 ), ( 10 15 3 20 )
LEN=${#A1[@]}; [ $LEN -lt ${#A2[@]} ] && LEN=${#A2[@]}
while [ 0$i -lt $LEN ]; do [ 0"${A1[$i]}" -gt 0"${A2[$i]}" ] && break; ((i++)); done; [ $i -eq $LEN ] && echo A2 || echo A1
A1

# 7  
Old 08-26-2014
Thanks guys! That's exactly what I needed! Works miracles! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare bash arrays issue

Hello everyone, I need help comparing 2 arrays. the first array is static; the second array is not .. array1=( "macOS Mojave" "iTunes" ) cd /Volumes array2=( * ) # output of array2 macOS Mojave iTunes Mac me The problem occurs when I compare the arrays with the following code - ... (6 Replies)
Discussion started by: trexthurman
6 Replies

2. Shell Programming and Scripting

How to compare decimal values in bash?

Hi, I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out. I have tried using below scripts. But its giving me error. 1)amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }') if ;then echo "correct" else echo "Wrong"... (3 Replies)
Discussion started by: Siba Tripathy
3 Replies

3. Shell Programming and Scripting

Compare two arrays

Hi, I am trying to compare two lists that are held in two variables so I believe I need to access the array elements to compare these. I am using ksh 88 and the code I have tried is below: for file in ${origfilelist} do if ]] then print -- "File ${file}... (3 Replies)
Discussion started by: frodo61
3 Replies

4. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

7. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

8. Shell Programming and Scripting

Using Bash scripting to compare arrays looking for matches

I have two arrays I need to compare against a third, looking for matches, not differences. I think I'm going to have to convert the arrays to files and grep them, but I'm not too sure if there's a tool to enable me to matches specifically, instead of differences. Thanks in advance! Karl (9 Replies)
Discussion started by: karlp
9 Replies

9. Shell Programming and Scripting

compare/match arrays

Hi there all, I am having a question. Is it posible to compare elements of 2 different arrays? For example I got Array 1 | Array 2 123_abc | 123_bcd 123_bcd | 234_bcd 234_abc | 567_abc 234_bcd | 123_abc than the match is 123_abc & 234_bcd and non of the others. So... (3 Replies)
Discussion started by: draco
3 Replies

10. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies
Login or Register to Ask a Question