5

I have to compare two files, file1 and file2. Each file has 56 columns separated by |. First column is the employee number in the file, I will check whether same employee number is present in the second file or not. If not we will write the whole row to the output file. If same employee number is present in the file2, I need to compare value of each column. If data doesn't match, we have to write it to the output file. If values of each column match then we need to omit that record.

Sample File
File 1

2620|256034|131021|Mission Quality and Wipro Way|||
2622|256034|131021|Mission Quality and Wipro Way|||
2623|256034|131021|Mission Quality and Wipro Way|||

File 2

2620|256034|234567|Mission Quality and Wipro Way|||
2621|256034|131021|Mission Quality and Wipro Way|||
2622|256034|131021|Mission Quality|||
2623|256034|131021|Mission Quality and Wipro Way|||

Sample Output:

2620|256034|131021|Mission Quality and Wipro Way|||
2621|256034|131021|Mission Quality and Wipro Way|||
2622|256034|131021|Mission Quality|||
5

If files are sorted much easy do the task by diff

comm -13 File1 File2
  • Can you please share a sample script for doing this. – Biswamohan Padhy Dec 26 '14 at 13:50
  • @BiswamohanPadhy I've take samples in your question. – Costas Dec 26 '14 at 13:58
0

You could use awk e.g.

awk -F'|' 'NR==FNR {a[$1]=$0;next;}; !($1 in a) || ($0 != a[$1]);' File1 File2

Using your example File1 and File2

$ awk -F'|' 'NR==FNR {a[$1]=$0;next;}; !($1 in a) || ($0 != a[$1]);' File1 File2
2620|256034|234567|Mission Quality and Wipro Way|||
2621|256034|131021|Mission Quality and Wipro Way|||
2622|256034|131021|Mission Quality|||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.