Combining Two Files

Could someone help me reduce the number of runs for a shell program I created?

I have two text files below:

$ more list1.txt
01 AAA
02 BBB
03 CCC
04 DDD

$ more list2.txt
01 EEE
02 FFF
03 GGG

I want to combine the lines with the same number to get the below:

01 AAA 01 EEE
02 BBB 02 FFF
03 CCC 03 GGG

I made a shell which does this. The number of runs for this shell is the product of the number of lines in input1 and input2. I have a very large input and when I used this utility it took a long time to process and was wondering if there's another method to do this with less number of runs.

$ more combine.ksh
#!/bin/ksh

while read number1 text1
do
  while read number2 text2
    do
       [[ $number1 = $number2 ]] && echo "$number1 $text1 $number2 $text2"
    done < $2
done < $1

you can use simply paste file1 file2. This works fast also and hope it will resolve your problem.

Regards,
Manish Jha.

see man pages for "paste" I guess it should work

Here is a certain improvement to your script.

[[ $number1 = $number2 ]] && echo "$number1 $text1 $number2 $text2" && break;

It will skip those succeeding elements after a match is found.

You might want to think of putting the elements of list2 in an array and then remove them dynamically (if it can be done..). All of these put together, I think you are better off with paste.

Thanks guys!!

I was able to reduce it to one line below:

paste $1 $2 | grep ".* .* .*"