Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

# Generated by NetworkManager domain dhcp.example.com search dhcp.example.com nameserver 10.0.0.1 nameserver 10.0.0.2 nameserver 10.0.0.3 

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:

# Generated by NetworkManager domain dhcp.example.com search dhcp.example.com nameserver 127.0.0.1 nameserver 10.0.0.1 nameserver 10.0.0.2 nameserver 10.0.0.3 

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?

like image 750
PHLAK Avatar asked Jul 18 '11 20:07

PHLAK


People also ask

How do I add a new line to a text file in bash?

Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file. How these commands can be used in the bash script are shown in this article.

How do I add a line break in bash?

Printing Newline in Bash Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.

How do I add a line to a file?

There are two standard ways of appending lines to the end of a file: the “>>” redirection operator and the tee command. Both are used interchangeably, but tee's syntax is more verbose and allows for extended operations.


2 Answers

Here is a solution using sed:

$ sed -n 'H;${x;s/^\n//;s/nameserver .*$/nameserver 127.0.0.1\n&/;p;}' resolv.conf  # Generated by NetworkManager domain dhcp.example.com search dhcp.example.com nameserver 127.0.0.1 nameserver 10.0.0.1 nameserver 10.0.0.2 nameserver 10.0.0.3 

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:

H 

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

x 

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

s/^\n// 

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 &):

s/nameserver .*$/nameserver 127.0.0.1\n&/ 

Now we just need to print the results:

p 
like image 197
brandizzi Avatar answered Oct 04 '22 17:10

brandizzi


Assuming you want to insert immediately after the search line, this is much simpler:

sed -ie '/^search/a nameserver 127.0.0.1' filename 
  • -i : edit file in place
  • -e : allows the execution of a script/commands inside sed expression
  • a mynewtext : command that tells sed to insert the text mynewtext after matched pattern
like image 33
Jim Garrison Avatar answered Oct 04 '22 19:10

Jim Garrison