Compare files in a folder based on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare files in a folder based on another file
# 1  
Old 06-04-2013
Compare files in a folder based on another file

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

Code:
//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
Code:
67 9
89 5
234 9

The content of class2.txt file in folder1
Code:
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
Code:
67 9
89 5
234 9
45  0
90  0

class2.txt file in folder1
Code:
456  9
120  12
34   0
78   0
89   0

your help would be appreciated!!

Last edited by jaff rufus; 06-04-2013 at 09:43 AM..
# 2  
Old 06-04-2013
An awk solution:
Code:
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

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move files with a certain suffix based on how many files are in another folder

Hello, First time poster. I am looking for a way to script or program the process of moving files from one folder to another, automatically, based on the count of files in the destination folder. I was thinking a shell script would work, but am open to the suggestions of the experts... (6 Replies)
Discussion started by: comtech
6 Replies

2. Shell Programming and Scripting

Compare two files based on column

Hi, I have two files roughly 1200 fields in length for each row, sorted on the 2nd field. I need to compare based on that 2nd column between file1 and file2 and print lines that exist in both files into separate files (I can't guarantee that every line in file1 is in file2). Example: File1: ... (1 Reply)
Discussion started by: origon
1 Replies

3. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

4. Shell Programming and Scripting

Compare three files based on two fields

Guys, I tried searching on the internet and I couldn't get the answer for this problem. I have 3 files. First 2 fields of all of them are of same type, say they come from various databases but first two fields in the 3 files means the same. I need to verify the entries that are not present... (4 Replies)
Discussion started by: PikK45
4 Replies

5. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

6. Shell Programming and Scripting

Compare text file and a folder

Hi, I have a folder which contains some files a1.txt a2.txt a3.txt a4.txt a5.txt a6.txt a7.txt a8.txt a_index.txt Also i have a text file which contains entries like this a1.txt a3.txt a7.txt I want to comapare this text file and folder and remove the similar values in... (12 Replies)
Discussion started by: arijitsaha
12 Replies

7. Shell Programming and Scripting

compare 2 files based on columns

Hi Experts, Is there a way to compare 2 files by columns and print matching cases. I have 2 files as below, I want cases where col1 and col2 in f1 matches col1 and col2 in f2 to be printed as output. The separator is space. I want the output to have col1 col2 col 3 from both files printed... (7 Replies)
Discussion started by: novice_man
7 Replies

8. Shell Programming and Scripting

Compare columns of 2 files based on condition defined in a different file

I have a control file which tells me which are the fields in the files I need to compare and based on the values I need to print the exact value if key =Y and output is Y , or if output is Y/N then I need to print only Y if it matches or N if it does not match and if output =N , then skip the feild... (7 Replies)
Discussion started by: newtoawk
7 Replies

9. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

10. UNIX for Dummies Questions & Answers

How to compare the difference between a file and a folder??

Hi, I have a .txt file which has to be compared with a folder and print the difference to some other .txt file. I did try with the diff command..i mean diff /tmp/aaa/bbb.txt /space/aaa/bbb/ /***bbb.txt contains all the files names which may or may not exist in the folder bbb..so i need... (2 Replies)
Discussion started by: kumarsaravana_s
2 Replies
Login or Register to Ask a Question