Text Processing with sed/awk – Replace All Text After a Certain Line

awksedtext processing

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:

sed -n -e '1,/^### REPLACE EVERYTHING AFTER THIS LINE ###$/{ p; d; }' \
       -e 'r replacement_file.txt' \
       -e 'q' original_file.txt

The three sed blocks do this:

  1. The first block prints all lines from line 1 to the line with the special contents. I print these lines explicitly with p and then invoke d to force a new cycle to start ("print; next" in awk).
  2. After the initial lines have been outputted by the first block, the second block outputs the contents of the extra file.
  3. The editing script is then terminated.

Ordinarily, q in the third block would output the current line before quitting (this would be the line in the example data reading text_that_will), but since sed 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

common_text
### REPLACE EVERYTHING AFTER THIS LINE ###
testing123
this_is_the_replacement_text

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:

sed ... original_file.txt >original_file.txt.new &&
mv original_file.txt.new original_file.txt
Related Question