[Tutor] Add lines to middle of text file

Wolf Halton wolf.halton at gmail.com
Sun Aug 14 05:34:30 CEST 2011


Is there a way to add text to an arbitrary place in a text file?

<pseudocode>
NewAddition="this important directive. "

Open config.config
Add new line above "# this marker text in config.config"
Insert $NewAddition in blank line created above.
Save config.config
Close config.config
<\pseudocode>

I am automating adding servers to nagios monitoring. I already have the part
that creates the new server.cfg file. This was a fairly simple bash script.
I am planning to write it in python instead, so i can let the administrator
choose the services to enable in a series of if statements, rather than
having to hand-edit the resultant file.

I need my script to add this filename to the main nagios.cfg list of read
files, but it isn't going to work to do it as a straightforward append...
Unless I can append it to a serverlist file and insert that file into
nagios.cfg at the proper position...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110813/dcf19e4f/attachment-0001.html>


More information about the Tutor mailing list