Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to replace a substring in a line, but I don't know what that substring is. I do know how the line starts though. For example, DNS1=[...to be replaced....].

I tried:

sed -i 's/DNS1=*/DNS1="8.8.8.8"/' /etc/sysconfig/network-scripts/ifcfg-eth0

and the result was:

Before: DNS1="10.10.2.2"

After: DNS1="8.8.8.8""10.10.2.2"

What am I doing wrong...

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try:

sed -i 's/DNS1=.*/DNS1="8.8.8.8"/' /etc/sysconfig/network-scripts/ifcfg-eth0

The * operator means "match the preceding character 0 or more times". In your example the preceding character is =, therefore it will match DNS1= and nothing more, since there are no more sequential = characters, and replace that portion with DNS1="8.8.8.8", which generates the string DNS1="8.8.8.8""10.10.2.2", just as you said.

The . operator means "any character" in sed context, which is probably what you want here. That variation will match DNS1= and then "any character 0 or more times" until the end of the line and then replace that with DNS1="8.8.8.8".

share|improve this answer
    
that did the trick. I figured it was something simple like that, thanks. –  cHam Aug 1 '13 at 19:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.