On 2006-08-24, Bill Marcum wrote:
> On 24 Aug 2006 07:11:52 -0700, pedromalves@gmail.com
> <pedromalves@gmail.com> wrote:
>> I'm trying to do search/replace on multiple text files.
>> I'm using the following script:
>>
>> #!/bin/sh
>> USAGE=`basename $0`
>> USAGE=$USAGE' <PATTERN> <SUBSTITUTE> <FILES PATH>'
>> if test $# -lt 3
>> then
>> echo "-I-: $USAGE"
>> exit 1
>> fi
>> PATTERN=$1
> PATTERN="$1"
That doesn't make any difference.
>> shift
>> SUBSTITUTION=$1
> SUBSTITUTION="$1"
Ditto.
>> shift
>> for i;
>> do
>> mv $i $i.old;
> mv "$i" "$i.old"
>> sed -e 's/$PATTERN/$SUBSTITUTION/g' $i.old > $i;
>> done
>> \rm *.old
>>
>> What happens is that if I echo the commands out to the shell and
>> execute them, the replacement will occur as expect. If I run to script
>> to execute the sed command, the resulting output will not have any
>> change.
>>
>> Can someone please me understand this?
>>
> Variables are not substituted inside single quotes!
> sed -e "s/$PATTERN/$SUBSTITUTION/g" $i.old > $i;
That does.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence