Reputation: 105
i want to replace 2 entrys in conf file with sed
# Set Server
/bin/sed -i 's/server: 127.0.0.1/server: xxx.xxx.xxx.xxx/' /etc/cobbler/settings
# Set next Server
/bin/sed -i 's/next_server: 127.0.0.1/next_server: 192.168.122.1/' /etc/cobbler/settings
for some reason it changes the the primary entry on both, why ? i use 2 different patterns to check for "server" & "next_server"
also i would like to know how i could change a quoted string pattern
#change default password
/bin/sed -i 's/default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."/default_password_crypted: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/' /etc/cobbler/settings
thx
Upvotes: 4
Views: 145
Reputation: 1867
If your line server: 127.0.0.1
has no extra space or other string in the line,
you should add ^
and $
.
/bin/sed -i 's/^server: 127.0.0.1$/server: xxx.xxx.xxx.xxx/' /etc/cobbler/settings
And your second question, you need to escape $
.
/bin/sed -i 's;default_password_crypted: "\$1\$mF86/UHC\$WvcIcX2t6crBz2onWxyac.";default_password_crypted: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";' /etc/cobbler/settings
Upvotes: 1
Reputation: 3031
Just add a ^
to the beginning of your regexp to require the line starting with exactly that string.
# Set Server
/bin/sed -i 's/^server: 127.0.0.1/server: xxx.xxx.xxx.xxx/' /etc/cobbler/settings
# Set next Server
/bin/sed -i 's/^next_server: 127.0.0.1/next_server: 192.168.122.1/' /etc/cobbler/settings
Upvotes: 0
Reputation: 784948
It is happening because your first matching regex is:
server: 127.0.0.1
And 2nd one is:
next_server: 127.0.0.1
As you can see first regex will match both the patterns since server: 127.0.0.1
is found for both cases.
Fix: To avoid unwanted matching you need to use word boundaries like this:
sed -i.bak 's/\b\(server: \)127.0.0.1/\1xxx.xxx.xxx.xxx/' /etc/cobbler/settings
Note: On OSX you need to use this weird syntax for word boundaries:
sed -i.bak 's/[[:<:]]\(server: \)127.0.0.1/\1xxx.xxx.xxx.xxx/' /etc/cobbler/settings
Upvotes: 1