1751808 Members
3837 Online
108781 Solutions
New Discussion юеВ

Best sed usage

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

Best sed usage

Hi all,

I have 3 different parameters to change in a file and want to use sed, what is the best way to change the value of 3 different strings from the same file - then replace the file ie:

FILE=FILE.cfg
userbase=xyz
timeout=5
defaultentry=local

# cat $FILE | sed -e 's/userbase=xyz/userbase=abc/;
-e s/timeout=5/timeout=60/ -e s/defaultentry=local/defaultentry/' > ${FILE}.tmp

cp ${FILE}.tmp ${FILE}

is this the best way or is there a more logical method?

thanks

Chris
hello
10 REPLIES 10
James R. Ferguson
Acclaimed Contributor
Solution

Re: Best sed usage

Hi Chris:

If you want to do this with 'sed', do:

# sed -e 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' ${FILE} > ${FILE}.tmp

# mv ${FILE}.tmp ${FILE}

Notice that you do NOT need to run a 'cat' process which 'sed' then reads from a pipe. This is a waste of a process.

Notice too the change I made in the 'sed' syntax. A semicolon between the substitution commands with one '-e' switch ahead of the arguments.

Of course, Perl can do an inplace update eliminating the 'mv' step :-)

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Best sed usage

> Notice too the change I made in the 'sed'
> syntax.

Which makes it harder to read, but does it
have any other (real) benefits?
James R. Ferguson
Acclaimed Contributor

Re: Best sed usage

Hi (again):

> Steven: > Notice too the change I made in the 'sed' syntax. Which makes it harder to read, but does it have any other (real) benefits?

In my opinion, this isn't harder to read. To me, is is more consistent with a script and a script's syntax (which the '-e' signifies in the first place). Moreover, the syntax becomes exactly compatabile to a Perl script:

# perl -pe 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' file

...which then leads to doing Perl in-place updates, thereby saving the 'mv step:

# perl -pi.old -e 's/userbase=xyz/userbase=abc/;s/timeout=5/timeout=60/;s/defaultentry=local/defaultentry/' file

...which preserves the original file as "file.old" while leaving the updated file as "file".

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: Best sed usage

here is what I initally wrote:

userbasedn="userbasedn:ou=people,ou=nis,ou=eu,o=unilever,c=gb"
ldaptimeout="ldaptimeout:5"
defaultentrylocation="defaultentrylocation:local"

sed -e 's/userbasedn.*$/'${userbasedn}'/' -e 's/.ldaptimeout.*$/'${ldaptimeout}'/' -e 's/^defaultentrylocation.*$/'${defaulte
ntrylocation}'/' ${LDAPCFG} > ${LDAPCFGBCK}

and this works however your syntax the command waits ie:

sed -e 's/userbasedn.*$/'${userbasedn}'/';'s/.ldaptimeout.*$/'${ldaptimeout}'/';'s/^defaultentrylocation.*$/'${defaultentrylocation}'/' ${LDAPCFG} > ${LDAPCFGBCK}


what have i missed?

Thanks




hello
James R. Ferguson
Acclaimed Contributor

Re: Best sed usage

Hi (again) Chris:

Ah, so you changed the example :-)

OK, if you are going to use interpolated variables, put them in double-quotes so that the shell expands them:

# sed -e "s/userbasedn.*$/"${userbasedn}"/;s/.ldaptimeout.*$/"${ldaptimeout}"/;s/^defaultentrylocation.*$/"${defaultentrylocation}"/" file

Regards!

...JRF...


Steven Schweda
Honored Contributor

Re: Best sed usage

> In my opinion, this isn't harder to read.

Whateveryousay.
OldSchool
Honored Contributor

Re: Best sed usage

of course, the original posting:

sed -e 's/userbase=xyz/userbase=abc/;
-e s/timeout=5/timeout=60/ -e s/defaultentry=local/defaultentry/' > ${FILE}.tmp

doesn't / wont run anyway...it doesn't understand the "-e" inside the quotes...believe the o.p. is confusing this with the "multiple pattern" in grep
James R. Ferguson
Acclaimed Contributor

Re: Best sed usage

Hi Chris:

> OldSchool: of course, the original posting...doesn't / wont run anyway...it doesn't understand the "-e" inside the quotes...believe the o.p. is confusing this with the "multiple pattern" in grep

True, but this is legal (and looks like a multiple option/multiple pattern 'grep'):

# sed -e 's/userbase=xyz/userbase=abc/' -e 's/timeout=5/timeout=60/' -e 's/defaultentry=local/defaultentry/' file

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Best sed usage

>is this the best way or is there a more logical method?

Other than JRF's mv and removal of cat, I would go with multiple -e scripts that he had at the end.

I typically wouldn't use the jammed up embedded commands but having a blank after each ";" may make Steven happy. :-)

>Steven: Which makes it harder to read, but does it have any other (real) benefits?

Well, as OldSchool points out, JRF's works, and the original didn't. I would typically use multiple -e. You could put the script in a file (-f script-file) or use:
sed -e 's/userbase=xyz/userbase=abc/
s/timeout=5/timeout=60/
s/defaultentry=local/defaultentry/' ${FILE} > ${FILE}.tmp

A file wouldn't allow Lawrenzo's ${userbasedn} substitution, unless it was a here document.