[NTLUG:Discuss] sed question

fredjame fredjame at fredjame.cnc.net
Wed Apr 7 07:39:10 CDT 2004


fredjame wrote:

> 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
>>  
>>
> Just a couple of quickies off the top of my head ...
> (1) If the two lines in question are:
> (1-a) always in the same place, you could use the line numbers as the 
> addresses instead of the pattern
> (1-b) not always in the same place, is there anything else in the line 
> that is constant in addition to "server"?
> That is, can you make a unique identification of each line?  If you 
> can, you could do a search/replace.
> (2) Would you want to keep of history of the lines replaced/changed?
>
Just one more: If this is all embedding in a shell script (Bourne, 
BASH), grep could be used to get the line numbers.

-- 
...we are fellow passengers...





More information about the Discuss mailing list