Skip to content
Advertisement

sed command to replace all last lines with some new multi line strings after getting second pattern

I want to write a program to change systems ip address from dhcp/static to static only. my original file’s contents will be either this

JavaScript

or it may be like this,

JavaScript

Now, i want to change after [ipv4] pattern, so that after editing my file should looks like this

JavaScript

Advertisement

Answer

Here’s a Perl solution. It sets the record delimiter to 2 newlines, so paragraphs are read. If the paragraph contains [ipv4], replace the input with the replacement. Replace data with the name of your file, or with "$@" and make it into a shell script. Add -i as an option to Perl if you want to overwrite files (don’t do that until you’ve tested it).

JavaScript

There are many alternative ways of writing that code; I make no claims other than “it works for me”.

Given the text of your question as the input, the output is:

JavaScript
Advertisement