[NTLUG:Discuss] sed question

Burton M. Strauss III Burton_Strauss at comcast.net
Wed Apr 7 08:40:54 CDT 2004


Have you considered Awk?

BEGIN {
   if(ARGC != 3) {
     print "usage: awk -f script.awk ntp1 ntp2 <oldconf >newconf"
     exit(0);
   }
   ntp2=ARGV[2]
   ARGV[2]=""
   ntp1=ARGV[1]
   ARGV[1]=""
}

# Retain server 127.xxxx lines
/^server *127/ { print $0; next }

# Drop all other server lines
/^server/ { next }

# Echo anything else
{ print $0 }

END {
  # Now create our new server lines
  print "server " ntp1
  print "server " ntp2
}

(I haven't actually tried the code, but it or something like it should work)

-----Burton

> -----Original Message-----
> From: discuss-bounces at ntlug.org [mailto:discuss-bounces at ntlug.org]On
> Behalf Of Ralph Green, Jr
> Sent: Wednesday, April 07, 2004 2:47 AM
> To: NTLUG Discussion List
> Subject: [NTLUG:Discuss] sed question
>
>
> 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
>
>
>
>
>
> _______________________________________________
> https://ntlug.org/mailman/listinfo/discuss




More information about the Discuss mailing list