making code compatible to previous bash versions

First let me explain the scenario
I have tywo files as usual

file1.txt (it has n rows and 8 columns)
$1 $2 $3 $4 $5 $6 $7 $8

Code:

1234567|iufgt|iuoy|iout|white |black |red        |90879
1234567|iufgt|iuoy|iout|green |pink  |blue       |90879
1234567|iufgt|iuoy|iout|orange|purple|magenta|90879
1234567|iufgt|iuoy|iout|yellow |violet|grey      |90879

we have to consider here $5 , $6 , $7 for our search

file2.txt (it has n rows and 6 columns)

$1 $2 $3 $4 $5 $6

Code:

1234567|grey|iuoy|iout|iufgt|90879
1239877|magenta|iuoy|iout|iufgt|90879
1733267|blue|iuoy|iout|iufgt|90879
1232677|red|iuoy|iout|iufgt|90879
1239567|white|iuoy|iout|iufgt|90879
1238727|green|iuoy|iout|iufgt|90879
1237247|orange|iuoy|iout|iufgt|90879
1236397|yellow|iuoy|iout|iufgt|90879
1232947|pink|iuoy|iout|iufgt|90879
1230247|black|iuoy|iout|iufgt|90879
1234037|violet|iuoy|iout|iufgt|90879
1238037|purple|iuoy|iout|iufgt|90879
1237897||iuoy|iout|iufgt|90879
1238797||iuoy|iout|iufgt|90879
1239997||iuoy|iout|iufgt|90879

here we should take only $2 for comparison. As you can most of the $2 field records has value and some do not have value

Question:

I want to take the fields $5 , $6 , $7 from file 1 and compare it with $2 field from file 2. and the rsult should be like this

Code:
if $5 (file1) =$2 (file2) then replace $5 (file1) with $1 of (file2)
if $6 (file1) =$2 (file2) then replace $6 (file1) with $1 of (file2)
if $7 (file1) =$2 (file2) then replace $7 (file1) with $1 of (file2)
the final output will look like this

Actual file1.txt (before running the code)

$1 $2 $3 $4 $5 $6 $7 $8

Code:

1234567|iufgt|iuoy|iout|white |black |red    |90879
1234567|iufgt|iuoy|iout|green |pink  |blue   |90879
1234567|iufgt|iuoy|iout|orange|purple|magenta|90879
1234567|iufgt|iuoy|iout|yellow|violet|grey   |90879

FIle1.txt after running the above said condition

$1 $2 $3 $4 $5 $6 $7 $8

Code:

1234567|iufgt|iuoy|iout|1239567 |1230247 |1232677   |90879
1234567|iufgt|iuoy|iout|1238727 |1232947 |1733267   |90879
1234567|iufgt|iuoy|iout|1237247 |1238037 |1239877   |90879
1234567|iufgt|iuoy|iout|1236397 |1234037 |1234567   |90879

so the field $5 , $6 , $7 should get replaced from the matched valued of $1(file1)

Now for this i got the below code running in Bash 4.1 version perfectly

#!/bin/bash
while read line1; do
   array1=($(echo "$line1" | sed -e 's/|/ /g'))
   while read line2; do
       array2=($(echo "$line2" | sed -e 's/|/ /g'))
       if [[ "${array1[4]}" == "${array2[1]}" ]]; then
           array1[4]="${array2[0]}"
       fi
       if [[ "${array1[5]}" == "${array2[1]}" ]]; then
           array1[5]="${array2[0]}"
       fi
       if [[ "${array1[6]}" == "${array2[1]}" ]]; then
           array1[6]="${array2[0]}"
       fi
   done < file2.txt
   echo "${array1[*]}" >> output.txt
done < file1.txt

But the real problem occurs when i run the above code in system which is of bash version 3.00.16(1)-release (sparc-sun-solaris2.10)

I get error as man.sh: syntax error at line 3: `array1=' unexpected

Please help me in fixing this array error sooner as it is critical for me now :mad: , so that the script runs even in the 3.0 version too.

If your version of bash doesn't have arrays, there's not a lot to be done for that.

Do you have ksh available? You could initiailize an array in it like

set -A array1 $(echo "$line1" | sed -e 's/|/ /g')

You don't need to run sed once on each and every individual line by the way! That's not very efficient. Just set IFS="|" and then the shell will split on that, both in the BASH and KSH methods.

Oh ok. I tried in many possible ways yesterday and finally ended up in getting super confused. Thanks for the clarification can you please remodel the code in the way which is efficient and run on ksh.
Please help me out in fixing this script as it is very critical for my work :frowning:

Well, do you have ksh?

#!/bin/ksh
IFS="|"
while read LINE
do
        set -A array1 $LINE

        while read LINE2
        do
                set -A array2 $LINE2
        done < file2
done < file1