Useful Sed Tricks To Customize Configuration Files



A typical software installation goes similar this. You install the software using apt-get install or yum install.
Then, you lot manually edit the software's configuration file inward guild to satisfy your requirements. If you lot bring to repeat the install on multiple machines, this chop-chop becomes tedious.


Instead of manually editing the file, I run a text manipulation ascendancy such every bit sed or awk to brand the required changes. Then, I script the physical care for yesteryear inserting the commands inward a bash script file.


The scripting of configuration changes serves multiple purposes:


  • It is a permanent tape of the configuration changes.

  • It is readily repeatable on the same or a unlike machine.


Below, I illustrate ii sed tricks to brand configuration changes to the Apache webserver. The target configuration file is /etc/apache2/apache2.conf.


Before you lot brand whatever change, delight initiatory of all backup the master configuration file.



$ sudo cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf.orig


Replacing initiatory of all occurrence of a string


The default apache2.conf file contains the next line:


Timeout 300


Below is the sed ascendancy to alter the initiatory of all occurrence of Timeout inward the file to 100.



$ sudo sed -i "0,/^Timeout\s/ s/^Timeout\s\+[0-9]\+/Timeout 100/" /etc/apache2/apache2.conf


The -i parameter tells sed to edit the file inward house - that is, straight inward apache2.conf.


0,/^Timeout\s/ specifies the hit of lines over which the sed ascendancy is to hold upwards executed. In this example, the starting trouble is the initiatory of all trouble (line 0). The finishing trouble is the trouble returned yesteryear a search for the give-and-take Timeout which appears at the commencement of a trouble (^) in addition to followed yesteryear a whitespace (\s).


The trouble hit parameter limits the alter to alone the initiatory of all occurrence of Timeout inward the file. If you lot exit out the trouble range, each occurrence of Timeout inward the file volition hold upwards modified. In many scenarios, leaving it out is OK because the parameter occurs alone i time inward the configuration file.


For to a greater extent than or less configuration files, a parameter tin plow over off multiples times, inward unlike sections. Next, I illustrate how to boundary the alter to inside a detail department inward the configuration file.


Replacing a string inside a target section


The MaxClients parameter occurs inward iii sections inside the apache2.conf file:


  • mpm_prefork_module

  • mpm_worker_module

  • mpm_event_module


I desire to alter the MaxClients parameter inside the mpm_prefork_module only.


The default mpm_prefork_module is similar this:



<IfModule mpm_prefork_module>
StartServers five
MinSpareServers five
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>


Note that a department is delimited yesteryear the opening <IfModule> in addition to closing </IfModule> statements.


The next sed ascendancy modifies the value of MaxClients to eighteen inside the mpm_prefork_module section.

$ sudo sed -i "/<IfModule mpm_prefork_module>/,\@</IfModule>@ s/MaxClients\s\+[0-9]\+/MaxClients 18/" /etc/apache2/apache2.conf


The trouble hit is defined yesteryear the /<IfModule ... >/,\@</IfModule>@ clause inward the to a higher house statement. The opening trouble inward the trouble hit is specified yesteryear a search for the <IfModule ... > pattern. The closing trouble is specified yesteryear the search blueprint \@</IfModule>@.


An explanation of the closing trouble blueprint is warranted. The slash (/) grapheme is utilisation of the search blueprint for the closing trouble (</IfModule>). However, the slash is also the default delimiter for sed. Therefore, nosotros must utilisation a unlike delimiter (@) for the closing-line search pattern. Note that the initiatory of all @ is escaped (\@).


The s/MaxClients.../MaxClients 18/ clause changes the value of MaxClients to 18.


Conclusion


The to a higher house are examples of how you lot tin utilisation sed to script mutual scenarios of changing configuration files. You tin attain the same number using other tools such every bit awk or perl. Please utilisation the comment organization to allow us know your ain examples.


If you lot are interested to acquire to a greater extent than close sed, delight read my before posts on the tool:


Berlangganan update artikel terbaru via email:

0 Response to "Useful Sed Tricks To Customize Configuration Files"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel