-1

I wonder know if there is a way to edit a conf file without getting in the file and changing the lines?

In my case, I need to edit zabbix-agent conf file (located in /etc/zabbix/zabbix_agentd.conf) and there are some parameters in the file that I need to change, such as Server Name, DebugLevel, and others.

Normally I edit the file using vim and change the lines, but my idea is to edit the file directly from bash, but I don`t know if this is possible.

For example, if I need to change the parameter DebugLevel, at bash I would run:

# /etc/zabbix/zabbix_agentd.conf.DebugLevel=3

This actually doesn`t works, but I would like something like this for my problem...

Does anyone knows??

I tested what David said, but it didn`t solved my problem... There are some lines in the file that is commented and I need to uncomment them, and there are some lines that I just need to change.

For example, the line above:

# DebugLevel=3

I need to change to:

DebugLevel=3

And this line:

Server=127.0.0.1

I need to change for the IP of zabbix server name, like this:

Server=172.217.28.142

Is there any other way?

1 Answers1

0

If I understand your question correctly, then you want sed -i (the -i option allows sed to edit the file in place, and -i.bak will do the same but create a backup of the original file with the .bak extension)

To change DebugLevel=3 to DebugLevel=4 in file /etc/zabbix/zabbix_agentd.conf, you can do:

# sed -i.bak "/DebugLevel/s/[0-9][0-9]*$/4/" /etc/zabbix/zabbix_agentd.conf

If I misinterpreted your question, please let me know.

To Change Values at the End

Example Input File

$ cat file.txt
DebugLevel=3

Example Use

$ sed -i "/DebugLevel/s/[0-9][0-9]*$/4/" file.txt

$ cat file.txt
DebugLevel=4

To Remove Comments

You can do virtually the same thing to uncomment the parameters of interest, for example:

# sed -i.bak "/DebugLevel/s/^#//" /etc/zabbix/zabbix_agentd.conf

In each case, sed is searching for the label in the first set of forward slashes /.../, next the substitute command is called s and then the expression between the next set of forward slashes, e.g. /^#/ (^ match at the beginning), if matched, is replaced by what follows in the next set // (nothing in the remove comment case).

You will need to adjust the values as required to match each parameter you need to find and replace. Let me know if you have further problems and exactly what the problem is.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85