Tuesday, August 11, 2009

How to Search and Replace stirng using sed

I had to Edit file /etc/sysconfig/ntpd to add option -x in NTPS for instant Sync
cat /etc/sysconfig/ntpd
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid"

# Set to 'yes' to sync hw clock after successful ntpdate
SYNC_HWCLOCK=no

# Additional options for ntpdate
NTPDATE_OPTIONS=""

I used command
sed -i "s/OPTIONS=.*/OPTIONS=\"-x -g -u ntp:ntp -p \/var\/run\/ntpd.pid\"/" /etc/sysconfig/ntpd

but it was changing wherever OPTION is in Script i.e NTPDATE_OPTIONS was also getting changed
:s/old/new /1 dindnt work in sed

I searched and find out how to solve this
using ^ operator
This command ran fine
sed -i "s/^OPTIONS=.*/OPTIONS=\"-x -g -u ntp:ntp -p \/var\/run\/ntpd.pid\"/" /etc/sysconfig/ntpd

No comments: