1

In zabbix-agent.conf I have lines:

#   Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
Server=127.0.0.1

I want to replace line

Server=127.0.0.1

with my

Server=zabbix.mydomain.com

But if I do

sed -i -e 's/Server=127.0.0.1/Server=zabbix.mydomain.com/g' /etc/zabbix/zabbix_agentd.conf

it found also line with commented example and replace string in it. I get:

#<----->Example: Server=zabbix.mydomain.com,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
Server=zabbix.mydomain.com

How to replace only one line?

0

3 Answers 3

2

You need to

  • Match the text at the start of string
  • Escape the dots
  • Remove g flag since the match will only be found at the string start.

Also, you do not need the -e option, you can use

sed -i 's/^Server=127\.0\.0\.1/Server=zabbix.mydomain.com/' /etc/zabbix/zabbix_agentd.conf

See the online demo:

#!/bin/bash
s='#   Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
Server=127.0.0.1'
sed 's/^Server=127\.0\.0\.1/Server=zabbix.mydomain.com/g' <<< "$s"

Output:

   Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
Server=zabbix.mydomain.com
Sign up to request clarification or add additional context in comments.

Comments

2

This might work for you (GNU sed):

sed -i '/^Server=127\.0\.0\.1/cServer=zabbix.mydomain.com' file

Change line beginning Server=127.0.0.1 to Server=zabbix.mydomain.com.

Comments

1

The other answers are almost fine but they would also replace a line like:

Server=127.0.0.10

A complete solution with any sed could be:

sed -i 's/^Server=127\.0\.0\.1$/Server=zabbix.mydomain.com/' /etc/zabbix/zabbix_agentd.conf

^ and $ anchor the string to the beginning and end of line, respectively. Dots need backslash escape, else they stand for any character.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.