making code compatible to previous bash versions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting making code compatible to previous bash versions
# 1  
Old 05-09-2011
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:
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:
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:
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:
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

Code:
#!/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 Smilie , so that the script runs even in the 3.0 version too.

Last edited by Franklin52; 05-09-2011 at 04:18 AM.. Reason: Please use code tags
# 2  
Old 05-09-2011
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.
# 3  
Old 05-09-2011
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 Smilie
# 4  
Old 05-09-2011
Well, do you have ksh?

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Making a python package and cross-platform compatible

Hi Ive been trying for days now and i just cannot work this out. Can someone please tell me if im doing this right. I've written some python3.3 code and now i want to transfer it to an embedded computer to execute. My OS is a : Debian GNU/Linux 6.0.7 (squeezez) 32-bit kernel The... (1 Reply)
Discussion started by: RedEyedDog
1 Replies

2. Debian

Making a python package and cross-platform compatible

Hi Ive been trying for days now and i just cannot work this out. Can someone please tell me if im doing this right. I've written some python3.3 code and now i want to transfer it to an embedded computer to execute. My OS is a : Debian GNU/Linux 6.0.7 (squeezez) 32-bit kernel ... (0 Replies)
Discussion started by: RedEyedDog
0 Replies

3. Shell Programming and Scripting

Howto make ksh skript bash-compatible with backticks

I have the following ksh-script: #!/bin/ksh # Ueberprüfe, ob genau ein Parameter angegeben wurde test "$#" -eq "1" || { echo "USAGE: path_cleanup <PATH_NAME>"; return 1; } # Ueberpruefe, ob awk und nawk installiert sind test -x /bin/nawk || { echo "ERROR: nawk is not installed"; return 1;... (2 Replies)
Discussion started by: doc_symbiosis
2 Replies

4. Shell Programming and Scripting

Need a script to delete previous versions of files

Hi. I need a script (either bash or perl) that can delete previous versions of files. For instance, from our continuous build process I get directories such as build5_dev_1.21 build5_dev_1.22 build5_dev_1.23 build5_dev_1.24 I need a script that I can run every night (using "at"... (6 Replies)
Discussion started by: jbsimon000
6 Replies

5. HP-UX

Unable to compile ANSI compatible code on HP-UX

Hi!!, my HP-UX - 10.2 compiler doesnt appear to support ANSI style of coding. On compiling my C code, it flags error messages like error 1705: Function prototypes are an ANSI Feature Is it a generic problem with the HP compiler or do I need to use some special switches on the command... (9 Replies)
Discussion started by: jyotipg
9 Replies
Login or Register to Ask a Question