1748165 Members
3800 Online
108758 Solutions
New Discussion юеВ

Re: replace $2 using sed

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

replace $2 using sed

Hello,

is it possible to change the value of $2 using sed?

ie

MGTSYS=myserver

sed -e "/^server/ s/$2/${MGTSYS}/" etc/ntp.conf

??

Thanks

Chris.
hello
8 REPLIES 8
RUET
Regular Advisor

Re: replace $2 using sed

lawrenzo
by $2 , you mean second column ?
T G Manikandan
Honored Contributor
Solution

Re: replace $2 using sed

awk -F " " '/^server/ {$2="myserver";print }' ntp.conf
James R. Ferguson
Acclaimed Contributor

Re: replace $2 using sed

Hi Chris:

I suspect you mean:

# sed -e "s/\(server \)\(.*\)/\1${MGTSYS}/" /etc/ntp.conf

...as to change lines like:

server chris.xyz.com

to:

server lawrenzo.xyz.com

Regards!

...JRF...
RUET
Regular Advisor

Re: replace $2 using sed

if you really want sed :

cat ntp.conf| sed "/^server/s/\(server\) \(.*\)/\1 ${MGTSYS}/"
James R. Ferguson
Acclaimed Contributor

Re: replace $2 using sed

Hi:

> P.R. if you really want sed :

The 'cat' is useless overhead. Sed is quite capable of reading a file from the command line. You only added a useless process and an additional read and write of the input file into a pipe!

...JRF...
RUET
Regular Advisor

Re: replace $2 using sed

James

yes of course cat is not mandatory .. it was just a way of do it ..
you are right , sed can do the job alone ..

pat
lawrenzo_1
Super Advisor

Re: replace $2 using sed

ok got that - quite complex answers but something I can work with.

Thanks all

Chris.
hello
lawrenzo_1
Super Advisor

Re: replace $2 using sed

ty.
hello