0

I want to compare two pipe delimited text files and find out the difference between two files using Linux script and write/copy the difference to output file.

scenario 1

File 1

5756366|99|HOLDER, CTLY 6X12X15.5 PLST|HOLDER, CUTLERY 6X12X15.5 PLASTIC COPPER FINISH W/|INSERT|A
1516020|99|CHEESE, CRM GRDN VEG SPRED|CHEESE, CREAM GARDEN VEGETABLE SPREAD PLASTIC TUB|REF|A
5086368|99|MAT, CRPT 4'X6' OLEFN CHRCL|MAT, CARPET 4'X6' OLEFIN CHARCOAL CATALINA||A

File 2

5756366|99|HOLDER, CTLY 6X12X15.5 PLST|HOLDER, CUTLERY 6X12X15.5 PLASTIC COPPER FINISH W/|INSERT|A
1516020|99|CHEESE, CRM GRDN VEG SPRED|CHEESE, CREAM GARDEN VEGETABLE SPREAD PLASTIC TUB|REF|A
5086368|99|MAT, CRPT 4'X6' OLEFN CHRCL|MAT, CARPET 4'X6' OLEFIN CHARCOAL CATALINA||A
5639166|99|COVER, DOME TRAY 12X20 PCRBN|COVER, CLEAR SIDE CU|T OUT W/ HINGED DOOR LID|A

Here, I want to get the difference between two files. In this case, the file 2 is having extra record 5639166|99|COVER, DOME TRAY 12X20 PCRBN|COVER, CLEAR SIDE CU|T OUT W/ HINGED DOOR LID|A and this should be saved to a Diff File as shown below

Diff File

5639166|99|COVER, DOME TRAY 12X20 PCRBN|COVER, CLEAR SIDE CU|T OUT W/ HINGED DOOR LID|A

Scenario 2

File 1

5756366|99|HOLDER, CTLY 6X12X15.5 PLST|HOLDER, CUTLERY 6X12X15.5 PLASTIC COPPER FINISH W/|INSERT|A 1516020|99|CHEESE, CRM GRDN VEG SPRED|CHEESE, CREAM GARDEN VEGETABLE SPREAD PLASTIC TUB|REF|A

File 2

5756366|99|HOLDER, CTLY 6X12X15.5 PLST|HOLDER, CUTLERY 6X12X15.5 PLASTIC COPPER FINISH W/|INSERT|A 1516020|99|CHEESE, CRM GRDN VEG SPRED TEST|CHEESE, CREAM GARDEN VEGETABLE SPREAD PLASTIC TUB|REF|I

Here in this scenario, there is a change in the data for 1516020 record,So in the diff File i should have only 1516020 record as below

Diff File

1516020|99|CHEESE, CRM GRDN VEG SPRED TEST|CHEESE, CREAM GARDEN VEGETABLE SPREAD PLASTIC TUB|REF|I

Would any one please help me out with Linux script for the above scenarios.

4
  • So why can't you just look at the output of diff again?
    – DopeGhoti
    Mar 23 '20 at 20:58
  • 1
    (1) If you were dealing with files that said 17 cheese and 42 mouse, would you be able to do this yourself?  If yes, tell us how you would do it.  If not, why are you asking a question with an arbitrary extra complication?  Ask one question at a time, and do research first.  (2) If you were dealing with files that consisted of lines with under 200 characters, would that make the problem easier?  If not, use example data that people can look at and read, like 17|cheese and 42|mouse, rather than 200-character long lines full of capital letters. Mar 23 '20 at 21:13
  • @DopeGhoti: I guess that, when the user says “Diff File”, they mean “this is the output I want”, and not that they even know about the diff command. Mar 23 '20 at 21:13
  • Type man diff and man comm on your computer, try something and then come back with a question if you need help with whatever it is you tried.
    – Ed Morton
    Mar 23 '20 at 21:44
1

For scenario1 Below command will work, Tested and worked fine

awk 'NR==FNR{a[$0];next}!($0 in a){print $0}'  file1 file2 > diff

output

5639166|99|COVER, DOME TRAY 12X20 PCRBN|COVER, CLEAR SIDE CU|T OUT W/ HINGED DOOR LID|A
1
  • Thanks Praveen, That was a great help.
    – Diya
    Mar 25 '20 at 8:59

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.