Compare files in a folder based on another file

I have a file named file.txt that looks as follows

//class1.txt
45
234
67
89
90
//class2.txt
456
34
78
89
120

class1 and class2.txt are the names of files in a folder named folder1.

The content of class1.txt file in folder1

67 9
89 5
234 9

The content of class2.txt file in folder1

456  9
120  12

some numbers have missed in the first column of each file in folder1. I want to add that numbers in each file of folder1 based on the numbers in file.txt and also add zero as second column.

output

class1.txt file in folder1

67 9
89 5
234 9
45  0
90  0

class2.txt file in folder1

456  9
120  12
34   0
78   0
89   0

your help would be appreciated!!

An awk solution:

awk '
        /^\// {
                sub ( "//", X )
                F[$1]
                f = $1
                next
        }
        !/^\// {
                R[f","$1]
        }
        END {
                for ( k in F )
                {
                        while (( getline line  < k ) > 0 )
                        {
                                split ( line, V )
                                T[V[1]] = V[2]
                        }
                        for ( j in R )
                        {
                                split ( j, V, "," )
                                if ( V[1] == k )
                                        M[V[2]]
                        }
                        for ( l in M )
                        {
                                if ( l in T )
                                        print l, T[l] > k
                                else
                                        print l, "0" > k
                        }
                        close ( k )
                        split ( "", M )
                        split ( "", T )
                }
        }
' file.txt
1 Like