Gery
Gery

Reputation: 9036

Two variables comparison, one-to-four possible outputs

I have two variables:

a=`echo 262832 6469180`
b=`echo 262832 263159 6469180 6469390`

hence:

echo $a
262832 6469180

echo $b
262832 263159 6469180 6469390

"b" represents four UTM coordinates, which are arranged from west to east, and from south to north (ie, W E S N). Variable "b" contains "a" only in two positions. Thus, after the comparison between "b" and "a" I need to print "1" and "3". The problem is that I also need to replace "1" and "3" by "W" and "S".

I do the comparison as follows:

if [[ $b == *"$a"* ]]
then
   echo "a is in b"
else
   echo "not there"
fi

but don't get the right idea to do the output replacement (it should be by columns as far as I understand).

Any pointers are welcomed,

Upvotes: 1

Views: 73

Answers (1)

whoan
whoan

Reputation: 8521

You can do it easily with bash arrays:

wesn=( W E S N )
a=( 262832 6469180 )
b=( 262832 263159 6469180 6469390 )
for ((i=0; i < ${#b[@]}; i++)); do
    for ((j=0; j < ${#a[@]}; j++)); do
        [ "${b[i]}" == "${a[j]}" ] && echo "${wesn[i]}"
    done
done

It just compares member by member and returns letter mapped in wesn.

Upvotes: 2

Related Questions