-1

I have two files file1.txt and file2.txt

File1.txt contains below

SDGIQEGFLDCAJNHCKJHGAIUFCHLKQAJDCGQYFHCLKAH
`1531280O94E1-09E4=1-203E]O12-9E81098ER13
kjsghdjigqiugtfdiasjclkkahfoiuqhfok;jasdl
shdashlkcdkdalfjvhadjfk;asljkfcjda;kflkdjf
kahfdahfkadlkfkl;adj;fljad;hf;jda;fkdfljaklfh

File2.txt contains below

SDGIQEGFLDCAJNHCKJHGAIUFCHLKQAJDCGQYFHCLKAH
`1531280O94E1-09E4=1-203E]O12-9E81098ER13
kjsghdjigqiugtfdiasjclkkahfoiuqhfok;jasdl

i want to compare file1.txt with file2.txt line by line and if difference is found then add those line in file2.txt itself.

The expected output

File2.txt should look like below

SDGIQEGFLDCAJNHCKJHGAIUFCHLKQAJDCGQYFHCLKAH
`1531280O94E1-09E4=1-203E]O12-9E81098ER13
kjsghdjigqiugtfdiasjclkkahfoiuqhfok;jasdl
shdashlkcdkdalfjvhadjfk;asljkfcjda;kflkdjf
kahfdahfkadlkfkl;adj;fljad;hf;jda;fkdfljaklfh

NOTE: I want to merge only the difference not all the contents of file1.txt.

Is there any possible way to do that using shell scripting?

0

If you just want to add to the end of file2.txt, you can extract lines in file1 but not in file2 then append it to file2.

comm -23 file1.txt file2.txt >> file2.txt
  • 1
    Its not like that i want to add at the end of file2.txt. i want to compare file2.txt line by line with file1.txt and add those lines at the specific line number of file2.txt and both of my file contains huge data. The output is just the sample. – Deep Mukherjee Apr 7 '18 at 10:46
0

Simple join command:

join -a1 --nocheck-order file1.txt file2.txt
  • a FILENUM - also print unpairable lines from file FILENUM

The output:

SDGIQEGFLDCAJNHCKJHGAIUFCHLKQAJDCGQYFHCLKAH
`1531280O94E1-09E4=1-203E]O12-9E81098ER13
kjsghdjigqiugtfdiasjclkkahfoiuqhfok;jasdl
shdashlkcdkdalfjvhadjfk;asljkfcjda;kflkdjf
kahfdahfkadlkfkl;adj;fljad;hf;jda;fkdfljaklfh
0

first to test:

{ cat file2; grep -Fxvf file2 file1; }
SDGIQEGFLDCAJNHCKJHGAIUFCHLKQAJDCGQYFHCLKAH
`1531280O94E1-09E4=1-203E]O12-9E81098ER13
kjsghdjigqiugtfdiasjclkkahfoiuqhfok;jasdl
shdashlkcdkdalfjvhadjfk;asljkfcjda;kflkdjf
kahfdahfkadlkfkl;adj;fljad;hf;jda;fkdfljaklfh

After you ensure above is what you want, redirect the output to file2 itself.

grep -Fxvf file2 file1 >> file2

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.