How to create a script to compare 2 files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create a script to compare 2 files?
# 1  
Old 09-20-2013
How to create a script to compare 2 files?

I have two files
File1 and File2 will contains same number of fields.
The rows in File1 can be found anywhere in File2.
If any rows from File1 doesn't match to File2 then write those records to a File1 log file.
If any rows from File2 doesn't match to File1 then write those records to a File2 log file.
How would you script this in Unix Shell script?

File1 contains:
Code:
105|ALL|VIM|1
100|ALL|STM|3
99|CLL|TRA|2

File2 contains:
Code:
99|ALL|TRA|2
100|ALL|STM|3
75|MLL|STM|1

Desire results:
A file1 log should contain following records because it didn't find it in file2.
Code:
105|ALL|VIM|1
99|CLL|TRA|2

A file2 log should contain following records because it didn't find it in file1.
Code:
99|ALL|TRA|2
75|MLL|STM|1


Last edited by Scott; 09-20-2013 at 04:35 PM.. Reason: Code tags
# 2  
Old 09-20-2013
Hi
not found in file2:
Code:
sort file2 file2 file1 | uniq -u

not found in file1:
Code:
sort file1 file1 file2 | uniq -u

Regards.
# 3  
Old 09-23-2013
How do you write the records that didn't match to a logfile?
# 4  
Old 09-23-2013
Code:
awk 'FNR==NR{a[$1];next} ! ($1 in a)' file2 file1 > file1.log

cat file1.log
105|ALL|VIM|1
99|CLL|TRA|2

awk 'FNR==NR{a[$1];next} ! ($1 in a)' file1 file2 > file2.log

cat file2.log
99|ALL|TRA|2
75|MLL|STM|1

# 5  
Old 09-23-2013
Quote:
Originally Posted by ranjanp
How do you write the records that didn't match to a logfile?
Redirect stdout to resp. logfile. (c.f. man bash (or whatever shell you use))
# 6  
Old 09-23-2013
I have a third file that I need to compare, which contains following data:

Code:
MAT1|99|ALL|TRA|2
MAT1|100|ALL|STM|3
MAT1|75|MLL|STM|1

How can I compare the file1 with this file3?
File1 only contains 4 columns and file3 contains 5 columns, while comparing need
to strip off the 1st column on file3.

Last edited by Scott; 09-23-2013 at 09:36 PM.. Reason: CODE tags
# 7  
Old 09-24-2013
Try this for all file pairs; both respective log files will be created:
Code:
awk     'NF == 5        {sub (/^[^|]*\|/,"")}
         NR == FNR      {a[$0]; fn=FILENAME".log"; next}
         ($0 in a)      {delete a[$0]; next}
                        {print > FILENAME".log"}
         END            {for (i in a) print i > fn}
        ' FS="|" file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

How to create or convert to pdf files from csv files using shell script?

Hi, Can anyone help me how to convert a .csv file to a .pdf file using shell script Thanks (2 Replies)
Discussion started by: ssk250
2 Replies

3. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

4. UNIX for Dummies Questions & Answers

Unix Script to compare two files

Hello, I have a dat file nctilllist.dat which will be present in the directory path "/usr/lpp/web-data/mfg/nct/file-data/nctilllist.dat" nctillist.dat will have reference to files like DP100001.jpg,DP10002.PDF,DP100003.doc on the path /usr/lpp/web-data/mfg/nct/file-data will have... (12 Replies)
Discussion started by: gayathrivm
12 Replies

5. Shell Programming and Scripting

Shell Script to Compare Two Files

I have a directory with about 6 files that we receive regularly. these 6 files contain information for 3 different units, 2 for each unit. files related to a specific unit are named similarly with a change in number at the end of the file. the numbers should be sequential. for each grouping of... (3 Replies)
Discussion started by: scriptman237
3 Replies

6. Shell Programming and Scripting

Compare two csv files by two colums and create third file combining data from them.

I've got two large csv text table files with different number of columns each. I have to compare them based on first two columns and create resulting file that would in case of matched first two columns include all values from first one and all values (except first two colums) from second one. I... (5 Replies)
Discussion started by: agb2008
5 Replies

7. Shell Programming and Scripting

compare files and create new with awk

hi everybody: Anybody know how I can compare two files where the pattern would be the three firsts columns of each file to create another one. This is, I have two file: file1 is like: 20030601 14 05 498.985 0.436532 20030601 14 10 499.531 0.260819 20030601 14 15 499.427 0.508597... (7 Replies)
Discussion started by: tonet
7 Replies

8. UNIX for Dummies Questions & Answers

How to compare several files and create a new one

Hello, I have a few files with simple data on them. All of the files may contain the same and different data. I need to create a new file that contains data that is on all files at the same time. Let say if word "ffffffff" is on all files it must be in the new file. If word "kkkkkkk" is not on... (1 Reply)
Discussion started by: alxkn
1 Replies

9. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies
Login or Register to Ask a Question