Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have tried using regular diff and patch to do what follows, but it doesn't seem to work or I am not using it right. I am wondering if there is a tool to patch changes to a file based on the actual line content.
For example, I have the following similar files with common lines but different line orderings:

  File 1.txt
  Hi, my name is Ted.
  1
  2
  3
  4

  File 2.txt
  1
  2
  3
  4
  Hi, my name is Ted


  File 3.txt
  1
  2
  Hi, my name is Ted
  3
  4


  I change File 1.txt to:
  Hi, my name is Joe.
  1
  2
  3
  40

and now I want to create some sort of patch or distribute this change to the other files, but not by coping the entire contents of File 1.txt to the other files, but by chaning only common lines:

  4 

to

  40

and

  Hi, my name is Ted. 

to

  Hi, my name is Joe.

Any help appreciated,

Ted

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Different OSes may have totally different tools

Sed (exist on *nix, ported to Win, know nothing abt OSX)

From example

 # substitute (find and replace) "foo" with "bar" on each line

 sed 's/foo/bar/'             # replaces only 1st instance in a line

i.e. smth. like

sed 's/4/40/' 2.txt > 2e.txt
...
sed 's/4/40/' 4.txt > 4e.txt

or

sed 's/4/40/' 2.txt 3.txt 4.txt> common.txt

and split merged common.txt after

diff (see above)

Prepare two intermediate-edited versions of 1.txt

1a will be

  Hi, my name is Joe.
  1
  2
  3
  4

1b will be

  Hi, my name is Ted.
  1
  2
  3
  40

Prepare patches

diff -U 0 1.txt 1a.txt > fix1.patch

diff -U 0 1.txt 1b.txt > fix2.patch

Apply patches

patch -u -b 2.txt fix1.patch
...
patch -u -b 3.txt fix2.patch

Results of commands from head was not tested

awk script (see above)

GrepWin (Windows only)

Write search-replace pattern (regexps supported), test, store in presets

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.