[NTLUG:Discuss] sed question

Ralph Green, Jr severian at mail.joimail.com
Wed Apr 7 02:47:16 CDT 2004


Howdy,
  sed is one of those neat tools I always thought I should know, but
never managed to use.  I have a good application for it now and there is
one thing I have not figured out.  If you have a suggestion, please let
me know.  If you think this is the wrong tool, I'll listen to that.  I
want to do this in an efficient way.

  The problem is this.  I have a web page written in PHP.  It allows the
user to specify two NTP servers to use to keep the machine
synchronized.  When a user changes the servers on the web page, I need
to update the /etc/ntp.conf file and then restart the NTP service.  My
only probleem is updating the config file.  The two lines that need to
be changed of /etc/ntp.conf are shown here between the dashed lines:
- - - - - - - - - - - - - - - - - - -
server  ntppub.tamu.edu
server  tick.usno.navy.mil
- - - - - - - - - - - - - - - - - - -
  There are a couple of things to keep in mind.
1. There is another line in the file that starts with server.  It says
server  127.127.1.0 or some such.  It needs to be left unchanged.
2. While I will know what the two server lines said the last time I
updated the file because I have database entries for them, the user
could have made manual changes.  The script need to work in any case.

 I know one way to solve this would be to issue a command like
sed '/server[ \t][^0-9]'/d <ntp.conf >ntp.new
 This would delete any active server lines and then I could append my
new ones to the bottom of the file.  I'll go with this plan if I have
too,It os not pretty, but it is functional.

  It looks like there might be a way to tell sed to go to the first
matching line and then insert text(my new server lines) and then delete
the matching lines.  This has the advantage that the server lines appear
in their normal place in the file, below the appropriate comment block. 
But, I have not gotten this to work.

  Do you understand what I am trying to accomplish?  Can you offer a
suggestion?
Thanks,
Ralph


  




More information about the Discuss mailing list