1752782 Members
6347 Online
108789 Solutions
New Discussion юеВ

sed help please

 
SOLVED
Go to solution
Maaz
Valued Contributor

sed help please

this attached script has to do two task
1, if there is no ip address of ntp server, then provide it in the /etc/ntp.conf

2, if /etc/ntp.conf contains the wrong IP address of NTP server, then fix it.

Problem:
following doesnt work

cat /etc/ntp.conf.org | sed 's/$CHECK_SERVER/$STRING/' > /etc/ntp.conf

i.e sed doesnt substitute the value of a variable $CHECK_SERVER with the value of another variable "$STRING"

please help
10 REPLIES 10
Fredrik.eriksson
Valued Contributor

Re: sed help please

One recommendation is to do a
echo "$CHECK_SERVER -> $STRING"
and see what the result give you.

I believe this is because in your CHECK_SERVER string you've done an thought error.

CHECK_SERVER=$(grep ^server /etc/ntp.conf | grep -v '127.127.1.0' | cut -f2 -d" ")
The cut will give you the result "192.168.0.1" instead of "server 192.168.0.1" which is what you really wanted.

Use the echo method above to verify this, but I think this is the reason.

Best regards
Fredrik Eriksson
Maaz
Valued Contributor

Re: sed help please

Hi Thanks Fredrik Eriksson for help/reply
and thanks for the suggestion

I did a test

# cat file
this is maaz
# cp file file.old

# echo $STRING1
this is maaz

# echo $STRING2
this is NOT maaz

# cat file.old | sed 's/$STRING1/$STRING2/' > file

# cat file
this is maaz

i.e no change in the 'file'

please help me in substituting the values of variables via 'sed'
Ivan Ferreira
Honored Contributor
Solution

Re: sed help please

Use double quote in your sed command:

sed "s/$STRING1/$STRING2/"

Single quotes won't expand the variables.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Fredrik.eriksson
Valued Contributor

Re: sed help please

Not entirely sure why this wouldn't work.
It should have if string1="this is maaz" and the file contains "this is maaz".

Something you should know thou is that sed regexps are case sensitive, if you add a "i" at the end (ex. s/pattern/replace/i) this will change the sed regexp to match case insensitive ("A" and "a" will be matched as the same character).

I would try to do this manually first as an experiment.
# sed -e "s/192.168.0.2/192.168.0.1/" /etc/ntp.conf
This won't change the file just the output given to you in your shell.

If this works I would try adding one of the variables.
# sed -e "s/$CHECK_SERVER/192.168.0.1/" /etc/ntp.conf

And so on untill you find the reason why it wasn't changed.

Also I would recommend using only sed to change the file.
# cp /etc/ntp.conf /etc/ntp.conf.org
# sed -i "s/$CHECK_SERVER/$STRING/" /etc/ntp.conf

-i tells sed to change the file with the search-replace-expression :)

Best regards
Fredrik Eriksson
Maaz
Valued Contributor

Re: sed help please

thanks Ivan Ferreira, yes double quotes works ;)
and thanks Dear Fredrik.eriksson for your continuous support and suggestions and excellent tips

another relevant question
the following code has to do two task
1, if there is no TIMEZONE set, then set it in /etc/sysconfig/clock
2, if there is wrong TIMEZONE in /etc/sysconfig/clock, then fix it

ok, now
the proper value of timezone in /etc/sysconfig/clock should be
TIMEZONE="Asia/Tashkent"

right now the value of timezone(wrong) in /etc/sysconfig/clock is
TIMEZONE="aa/tshknt"

#!/bin/bash
CLOCK=/etc/sysconfig/clock
TIMEZONE_STRING='TIMEZONE="Asia/Tashkent"'
CHECK_TIMEZONE=$(grep ^TIMEZONE $CLOCK)

if [ -z "$CHECK_TIMEZONE" ]; then echo "$TIMEZONE_STRING" >> $CLOCK
else cp "$CLOCK" /tmp/clock
cat /tmp/clock | sed "s/$CHECK_TIMEZONE/$TIMEZONE_STRING/" > $CLOCK
fi
exit

upon execution of the above code, I got the following error

sed: -e expression #1, char 36: unknown option to `s'
Fredrik.eriksson
Valued Contributor

Re: sed help please

Why not just always sed over it?

sed -i "s/TIMEZONE=\".*\"/TIMEZONE="Asia/Tashkent/" /etc/sysconfig/clock

Since you always want it to be the same way and not dependent on what the previous value was this would be a simpler solution.
This ofcourse sets the prerequisite that the variable string itself actually exists thou. Which it should.

Best regards
Fredrik Eriksson
Fredrik.eriksson
Valued Contributor

Re: sed help please

sry, I missed a vital part.

sed -i "s/TIMEZONE=\".*\"/TIMEZONE="Asia\/Tashkent/" /etc/sysconfig/clock
Maaz
Valued Contributor

Re: sed help please

once again thanks Dear Fredrik.eriksson ;)

>sry, I missed a vital part.

>sed -i "s/TIMEZONE=\".*\"/TIMEZONE="Asia\/Tashkent/" /etc/sysconfig/clock

Dear when I copy and paste the above "sed" on terminal, it didnt execute
# sed -i "s/TIMEZONE=\".*\"/TIMEZONE="Asia\/Tashkent/" /etc/sysconfig/clock
>
(here I type ctrl+c, to get the prompt back)

but following works
# sed -i 's/TIMEZONE=\".*\"/TIMEZONE="Asia\/Tashkent/' /etc/sysconfig/clock

i.e i used ' instead of "

thanks for your kind support,
Fredrik.eriksson
Valued Contributor

Re: sed help please

It's another typo.

sed -i "s/TIMEZONE=\".*\"/TIMEZONE=\"Asia\/Tashkent\"/" /etc/sysconfig/clock

You need to escape qoutes that is inside of the search-replace expression.

Best regards
Fredrik Eriksson