1832663 Members
3045 Online
110043 Solutions
New Discussion

sed

 
Matt Dunfee
Occasional Contributor

sed

I'm looking to use the 'sed' command to update a simple text file, but am having a little trouble with the syntax.
Ex. text file:
SERVER = miami
DOMAIN = florida

I need to update the SERVER entry with a variable parameter that is passed into the script. I've tried to use /s for substitution, but I can't subsitute for "SERVER = miami" since 'miami' may not always be the value. Also, I've found that wildcards aren't accepted. The only option I see would be to use the 'c\' option with sed, but I may run into the same problem. Any suggestions?

Any help is appreciated.
Thanks in advance!
-Matt
3 REPLIES 3
John Poff
Honored Contributor

Re: sed

Hello Matt,

Here is one way to try it. I wrote a little script named fixit that takes the old server name and the new server name as parameters. It looks like this:

#!/bin/sh
# fixit

OLDSRV=$1
NEWSRV=$2

sed "s/SERVER = $OLDSRV/SERVER = $NEWSRV/" test.txt


(Where test.txt is the name of the file with your text.)


Then you can run it like this:

fixit miami tampa

And you get something like this:

SERVER = tampa
DOMAIN = florida


I'm sure the other wizards will have even cooler ways to do it, but that is a start.

JP

Re: sed

Assuming the new value is in variable NEWSERVER:

sed "/SERVER/s/=.*/=$NEWSERVER/"

Note double quotes and the period before * (wildcards in sed are different than in shell).
Praveen Bezawada
Respected Contributor

Re: sed

Hi
You can have something like, in the script ( a.sh )

sed 's/^\(SERVER = \).*/\1'$1'/' a.txt

you can run it like

./a.sh newserver

The newserver name will replac the old name.

..BPK...