Using sed to replace strings if NOT found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to replace strings if NOT found
# 1  
Old 05-07-2012
Using sed to replace strings if NOT found

Dear expert,

I need an urgent help.

I would like to update my /etc/ntp.conf file using sed.

1) if script find this string "127.127.1.0" then add the lone below
Code:
#server 127.127.1.0

2) is script find this string "fudge 127.127.1.0 stratum 10" then add
Code:
#fudge 127.127.1.0 stratum 10

2) if there is no "server 192.168.122.76" then add
Code:
server 192.168.122.76

at the end, save the new content into thesame file /etc/ntp.conf

Below is my sript:

My script below deosn't seems to work. Every time i run /etc/ntp.conf never get update

# Preparing the clock to synchronize with Network clock
Code:
const='tinker panic 0'
if [ -e /etc/ntp.conf ]; then
  grep -q -e "$const" /etc/ntp.conf
  if [ $? = 1 ]; then
    echo "$const  ; not found but will be inserted as first line!"
    sed -i.bak '1 i\tinker panic 0' /etc/ntp.conf
  fi
# Add '#' to beginning of line if it is not already there
# to disable the local clock.
sed -i.bak \
    -e 's/^server 127.127.1.0$/#server 127.127.1.0/' \
    -e 's/^fudge  127.127.1.0 stratum 10$/#fudge 127.127.1.0 stratum 10/' \
    -e 's/^server 192.168.122.76$/server 192.168.122.76/' /etc/ntp.conf
fi

Much appreciate!
Thank you!

Last edited by Scrutinizer; 05-07-2012 at 06:26 PM.. Reason: code tags
# 2  
Old 05-07-2012
Could you show the input you have and the output you want?
# 3  
Old 05-08-2012
1)input that i have: 127.127.1.0
input that iwant: #127.127.1.0 (only if exist)

2) input that i have: fudge 127.127.1.0 stratum 10
output that i want: #fudge 127.127.1.0 stratum 10

3) the string that iwant to add if NOT exist: server 192.168.122.76

this is the file that need to be upadated /etc/ntp.conf

Many thanks,
# 4  
Old 05-08-2012
Yes... But what would the file look like, before, and then what would it look like, after. All together -- not in pieces.
# 5  
Old 05-08-2012
Sorry, /etc/ntp.conf file see below:

Code:
tinker panic 0
tinker panic TEST 0
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (pool.ntp.org: Join the NTP Pool!).
#server 0.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org
#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 key 42  # manycast client
# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-08-2012 at 01:16 PM..
# 6  
Old 05-08-2012
Is that before or after modification?

And given either one, what does its counterpart look like?
# 7  
Old 05-08-2012
We need to see snippets of TWO files - one before one after
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and lossless replace strings using sed?

Hello, I would like to replace all occurencies of long data types by others (coresponding int) using 'sed' in the extensive source code of a software package written for 32 bit CPUs. I must use regular expressions to avoid wrong replacements like s/unsigned]+long/ulong/gLeaving out... (2 Replies)
Discussion started by: Mick P. F.
2 Replies

2. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

3. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

4. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

5. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

6. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

7. Shell Programming and Scripting

Using sed to replace two different strings?

Hey everyone! Simple question - I am trying to use sed to replace two different strings. As it stands I can implement this as: sed -i 's/TIMEOUT//g' sed -i 's/null//g' And it works. However, is it possible to shrink that down into a single command? Will there be any performance benefits? (3 Replies)
Discussion started by: msarro
3 Replies

8. Shell Programming and Scripting

Help with sed search&replace between two strings

Hi, i read couple of threads here on forum, and googled about what bugs me, yet i still can't find solution. Problem is below. I need to change this string (with sed if it is possible): This is message text that is being quoted to look like this: This is message text that is being quotedI... (2 Replies)
Discussion started by: angrybb
2 Replies

9. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

10. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies
Login or Register to Ask a Question