1

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

orig.sas 

to match the contents of an updated file

new.sas

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):

3668c
label DBMS=xlsx replace;
.
3649,3665d
3643d

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 – Praveen Kumar BS Feb 20 '18 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. – steeldriver Feb 20 '18 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) – Jack Osborne Feb 26 '18 at 11:03
2

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

try

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

ed orig.sas < changes.txt

where

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

see man ed for further information.

edit

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

| improve this answer | |
  • 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! – Jack Osborne Feb 26 '18 at 11:14

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.