[NTLUG:Discuss] sed question
Val Harris
val.harris at comcast.net
Wed Apr 7 16:07:24 CDT 2004
Ralph Green, Jr wrote:
> 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
>
How about bracketing your Server Lines with unique text? For example:
# - - - Sync_Server_Start - - - - - - -
server ntppub.tamu.edu
server tick.usno.navy.mil
# - - - Sync_Server_End - - - - - - - -
Now, you just delete all lines from "Sync_Server_Start" to
"Sync_Server_End" and insert your new lines (keeping the labels,
of course.) You might also want to put a comment to the user not
to modify the lines between the labels, because it'll be automatically
over-written.
Hth,
Val
--
Val W. Harris val.harris at comcast.net
People who wait for all conditions to be perfect
before acting, never act.
More information about the Discuss
mailing list