[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Clueless with sed



On Sat, Jul 06, 2002 at 08:56:00AM -0700, Sean 'Shaleh' Perry wrote:
> 
> On 06-Jul-2002 Joop Stakenborg wrote:
> > I am trying to get a sed script to behave nicely.....
> > 
> > In a file (called 'config') with a line like:
> >      DEVEL_PREFIX = _ENVPATH_/$(TARGET_ARCH)-linux-uclibc
> > I would like to replace _ENVPATH_ with the current path, with the following 
> > line: BASEDIR=`pwd`; sed 's/_ENVPATH_/$BASEDIR/g' config
> > 
> > Here is the result:
> >      DEVEL_PREFIX = $BASEDIR/$(TARGET_ARCH)-linux-uclibc
> > Anyone have more experience with sed than I do?
> 
> you used single quotes.  In shell '$foo' means exactly dollar sign f o o.  Use
> "$foo" and the variable will be interpreted.
> 
> $ echo 'NAME said hi' | sed -e "s/NAME/$CVS_FULLNAME/"
> Sean 'Shaleh' Perry said hi
> $ echo 'NAME said hi' | sed -e 's/NAME/$CVS_FULLNAME/'
> $CVS_FULLNAME said hi

This is correct however it won't fully solve his problem.

Presumably $BASEDIR will contain slashes so after expansion the sed
command would be something like 's/_ENVPATH_//foo/bar/baz/g' which will
greatly confuse sed. The solution is the little know fact that it
doesn't matter what the separator character is. The first character
after the command (in this case 's') is the separator. When working with
paths I usually use a comma as the separator so what he needs to use is:

sed -e "s,_ENVPATH_,$BASEDIR,"

which will expand to:

sed -e "s,_ENVPATH_,/foo/bar/baz,"

and sed will be happy.

-- 
"Important principles must be inflexible."
		-- Abraham Lincoln (quoted by Ronald Reagan
		   in address to NRA in TX, quoted in
		   Osha Gray Davidson _Under Fire_ pg 38)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net


-- 
To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org



Reply to: