1

i have a file:

unreliable, random content, multiple lines
this_line_is_always_the_same_and_never_repeated_in_file
unreliable, random content, SINGLE line
unreliable, random content, multiple lines

each of the unreliable, random content, multiple lines is a random number of lines, with random text

i am trying to create a .patch file that adds 4 lines to the above file like this:

unreliable, random content, multiple lines
this_line_is_always_the_same_and_never_repeated_in_file
unreliable, random content, SINGLE line
my_new_line_1
my_new_line_2
...
unreliable, random content, multiple lines

(when i say random, it isnt really random, i just mean that no 2 files being patched will have the same things)

i have no idea how to add content 2 lines after a line,

any help would be appreciated

sorry if i havent formatted this question correctly

EDIT: just adding that i cant rely on line numbers either, and this is my first time trying to use diff and patch

1
  • This sounds like the wrong approach. Why not use some other tool sed or similar? Those are meant for pattern matching and could add content. While patch should apply a diff to a file. A diff is a line by line comparison. – Seth Feb 10 '17 at 7:45
0

A patch file in unified format is just a number of chunks prefixed by a line starting with @@, which gives the line number and length of the chunk in the old and new file, and the name of the old and new file at the beginning. So if e.g. the always_the_same_line is line number 30, the patch file would look like

--- old_file_name
+++ new_file_name
@@ -30,2 +30,4 @@
 this_line_is_always_the_same_and_never_repeated_in_file
 unreliable, random content, SINGLE line
+my_new_line_1
+my_new_line_2

The line number is easy to find with grep -n always_the_same_line, the beginning two lines including the "unreliable, random content, SINGLE line" as trailing context can be found with grep -A1 always_the_same_line, and now you just need a bit formatting using awk, perl or whatever you are most comfortable with to produce the patch file in the required format.

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.