Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .