Skip to content
Advertisement

How do I add a line of text to the middle of a file using bash?

I’m trying to add a line of text to the middle of a text file in a bash script. Specifically I’m trying add a nameserver to my /etc/resolv.conf file. As it stands, resolv.conf looks like this:

JavaScript

My goal is to add nameserver 127.0.0.1 above all other nameserver lines, but below any text above that. In the end I want to my resolve.conf file to look like this:

JavaScript

How is this possible via a bash script? Is this something sed or awk can do? Or would creative greping to recreate the file be my best move?

Advertisement

Answer

Here is a solution using sed:

JavaScript

How it works: first, suppress the output of sed with the -n flag. Then, for each line, we append the line to the hold space, separating them with newlines:

JavaScript

When we come to the end of the file (addressed by $) we move the content of the hold space to the pattern space:

JavaScript

If the first line in pattern space is blank we replace it with nothing.

JavaScript

Then we replace the first line starting with nameserver by a line containing nameserver 127.0.0.1, a new line (Your version of sed may not support n, in which case replace the n with a literal newline) and the original line (represented by &):

JavaScript

Now we just need to print the results:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement