I'm currently attempting to change the contents of one file


to match the contents of an updated file


Replacing orig.sas with new.sas makes some of the changes, but there are many directories referenced within orig.sas that I do not want altered.

Submitting the following code outputs a script 'changes.txt' with all of the lines needed to be altered in orig.sas to make it identical to new.sas

diff -e orig.sas new.sas > changes.txt

changes.txt looks like this (full output has many more lines):

label DBMS=xlsx replace;

From here I have removed all lines which reference changes in directories, so only the body of the code is affected.

I think I should be able to submit this script into sed (with the aim of making all of the changes changes.txt) using something like:

sed - orig.sas < changes.txt

But I don't want to directly overwrite the orig.sas, instead preferring to output to a version 2 file (eg. orig_v2.sas)

My progress to this point has come from instruction from https://www.computerhope.com/unix/udiff.htm

  • we can help you out please post the sample input and expected output with clear explaination Commented Feb 20, 2018 at 13:57
  • 1
    The instructions you are following seem to be about creating and running an ed script, not a sed script - while superficially similar, there are going to be some differences. You might be able to do what you want using ed as described there but replacing the step echo "w" >> my-ed-script.txt with echo "w orig_v2.sas" >> my-ed-script.txt. Commented Feb 20, 2018 at 13:59
  • @steeldriver thank you very much for your solution! You were completely right, instructing ed to write to a newly named file was exactly what was needed. Very elegant, thank you! (I'm not sure how to give you the credit for your answer with a tick or vote) Commented Feb 26, 2018 at 11:03

1 Answer 1


diff -e 's result is supposed to be feed (modified) to ed.


diff -e orig.sas new.sas > changes.txt
echo wq >> changes.txt

ed orig.sas < changes.txt


  • wq will write and quit, printing number of char written
  • optional --silent will not output above count.

see man ed for further information.


I fail to see point in diffing to apply diff to original file, it looks to me that cp new.sas orig_v2.sas would do

I suppose you have this in mind : two branch alphaN and bravoN

diff -e alpha1.sas alpha2.sas > change12.txt

(cat change12.txt
 echo w bravo2.sas
 echo q ) | ed bravo.sas

this will report on bravoN changes made to alphaN

  • Thank you for your reply! The extra steps and suggestions are really appreciated. You are correct that copying the original file and then modifying it would work. The reason I was looking to write to a new output file (as in steeldriver's comment) is this is a process I'll likely perform regularly and want to reduce the chance of errors through fewer steps. I really like your suggestion of creating a changelog so will likely use that for auditing purposes. Thank you! Commented Feb 26, 2018 at 11:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .