I am currently trying to automate updating the text of a file, titled original_file.txt. Imagine the file looks like the following:
common_text
### REPLACE EVERYTHING AFTER THIS LINE ###
text_that_will
be_removed
after_the_command
This file will be updated by removing all text after "Replace everything after this line", and replacing it with the text in the file replacement_file.txt. For the sake of the post, imagine that replacement_file.txt has the following text:
testing123
this_is_the_replacement_text
From what I've been able to find with sed, I can only figure out how to edit the rest of the line after a certain phrase. I want to replace the text in original_file.txt after the replacement phrase with all of the text from replacement_file.txt (I want to keep the replace line text for future updates). original_file.txt should look like this at the end:
common_text
### REPLACE EVERYTHING AFTER THIS LINE ###
testing123
this_is_the_replacement_text
Thanks in advance!
Best Answer
Using
sed
:The three
sed
blocks do this:p
and then invoked
to force a new cycle to start ("print; next
" inawk
).Ordinarily,
q
in the third block would output the current line before quitting (this would be the line in the example data readingtext_that_will
), but sincesed
is invoked with-n
, this default outputting of a line at the end of a cycle is inhibited.The result of the above command, given your data, is
To update the original file, you could use
sed -i ...
, or redirect the output to a new file that you then replace the original with: