Sed replaces 2 different templates in conf

I want to replace 2 entries in a 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 master record on both, why? I use 2 different templates to check "server" and "next_server"

I would also like to know how I can change the 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

+4
source share
3 answers

Just add ^ to the beginning of your regular expression to require the line to start from that line.

 # 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 
0
source

This is because your first matching regular expression:

 server: 127.0.0.1 

And the second one:

 next_server: 127.0.0.1 

As you can see, the first regular expression will match both patterns, since server: 127.0.0.1 found for both cases.

Correction: To avoid unwanted matching , you need to use word boundaries as follows:

 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 strange syntax for word boundaries:

 sed -i.bak 's/[[:<:]]\(server: \)127.0.0.1/\1xxx.xxx.xxx.xxx/' /etc/cobbler/settings 
+1
source

If your line server: 127.0.0.1 does not contain an extra space or other line in the line, you must 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 exit $ .

 /bin/sed -i 's;default_password_crypted: "\$1\$mF86/UHC\$WvcIcX2t6crBz2onWxyac.";default_password_crypted: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";' /etc/cobbler/settings 
+1
source

Source: https://habr.com/ru/post/1491873/


All Articles