You can use diff -e or -ed option to output the difference to an ed script and then use the ed script the make the source file the same as the target file.
1 2 3 4 5 6 7 8 9 10 11 |
## Make both files consistant using diff -e and ed commands echo -e "1\n2\n3\n4" > myfile1 ## create some files echo -e "1\n2\n3\n4\n5" > myfile2 diff myfile1 myfile2 ## display the difference diff -e myfile1 myfile2 > myedscript ## creates an ed script cat myedscript echo "w" >> myedscript ## instruct ed to write to make the changes ed - myfile1 < myedscript ## execute the ed script on myfile1 cat myfile1 ## 5 added to myfile1 diff myfile1 myfile2 ## both files are now identical |