0

I've never seen sed behave like this but I'm trying to replace all instances of "nameserver" within /etc/resolv.conf with my own 3 entries, including the "nameserver". Every combo I've tried adds my 3 entries twice, so I have 6 entries.

sed -i 's/.*nameserver.*/nameserver 10.1.1.1\nnameserver 10.1.1.2\nnameserver 10.1.1.3/g' /etc/resolv.conf

which outputs

nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3
nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3

So, the correct order and entries but adding it twice. I've also tried narrowing it down to nameserver and ip but still get the same output printed twice.

sed -i "s/^nameserver [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/nameserver 10.1.1.1\nnameserver 10.1.1.2\nnameserver 10.1.1.3/g"

I'm really confused here and can't seem to get past this. Any help appreciated.

ETA: current resolv.conf

search domain1.net. domain2.net. domaine.com.
nameserver 192.168.1.1
nameserver 192.168.1.2

desired output

search domain1.net. domain2.net. domaine.com.
nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3
4
  • 2
    Please add your resolv.conf and your desired output for that sample input to your question (no comment). Commented Oct 13, 2019 at 17:55
  • Welcome to Stack Overflow! Please take the tour. I can't reproduce the problem with my resolv.conf, so you need to make a minimal reproducible example, like Cyrus mentioned. Commented Oct 13, 2019 at 18:15
  • You have two occurrences of nameserver in the original, so sed replaces each of them. Does that answer your question? Commented Oct 13, 2019 at 18:16
  • Not really. This script is going to be run on a few thousand hosts and they vary with 1 entry in resolv.conf to 3...I just want to replace every nameserver line with the new entries...No matter what I try I get 6 entries from my 3 that I want...So they're being doubled and I don't know why. Commented Oct 13, 2019 at 18:38

1 Answer 1

1

You have two occurrences of "nameserver" in the original, so sed replaces each of them.

You can do multiline replacements with sed, but it's simpler to delete the lines you don't want, then append the ones you do. Though I'm not familiar with /etc/resolv.conf so I don't know if order matters.

sed '/nameserver.*/d' /etc/resolv.conf
cat <<'EOF'
nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3
EOF

If the output looks good from this, run the below to actually make the changes:

sudo sed -i '/nameserver.*/d' /etc/resolv.conf
sudo tee -a /etc/resolv.conf >/dev/null <<'EOF'
nameserver 10.1.1.1
nameserver 10.1.1.2
nameserver 10.1.1.3
EOF
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Was able to get it working...still aggravated that I can't figure out why I'm getting multiple entries, even across different OS's (rhel6 and 7) but I've beat my head against it long enough...thanks again!
@cubezombie Welcome! I'm not sure why you're confused about the repeats though. Like I mentioned, sed replaces each of the matched lines.
Right, but in my original 2 examples I had 3 nameservers defined...it would create duplicate entries for each one leaving me with 6 nameserver entries in the order I had them but just doubled. You're method worked perfectly but it's the double entries in my earlier method I'm still confused about.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.