TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:10363] Using sed and evironmental variables




The following has worked for me for years. You just need to be
careful if the search or replace patterns contain ',' character
or other special (to the shell) meaning.

Regards

					- Karl

---------------------------------------------------------------------
#!/bin/sh
#
# just about the same as the following command
# perl -i -p -e s,search,replace,g <filename...>
#

search=$1
replace=$2
shift 2

while test $# -gt 0
do
        echo "Working $#: $1"
        ed - $1 <<-EOF
        1,\$s,${search},${replace},g
        w
        q
EOF
shift
done                         
---------------------------------------------------------------------



On Mon, 22 Nov 1999, Bob Tanner wrote:

> Is there a way to pass environmental variables into a sed script?
> 
> I have a env var called FOO=bob and the following sed script:
> 
> cat file | sed -e 's/FOO/$FOO/g'
> 
> I want to replace all occurances of "FOO" in file "file" with the value of the
> environmental variable FOO, which has a value of bob.
> 
> Using the above sed script, all occurances of "FOO" are replaced by "$FOO",
> not the bob (the value of $FOO).
> 
> Anyone?
>