Add line just before the first match

Friday, December 28, 2012 0 Comments


# 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

solution is :

awk '/^nameserver/ && !modif { printf("INSERT\n"); modif=1 } {print}'

0 comments: