PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Sed Find and Replace Multiple Files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Sed Find and Replace Multiple Files

Réponse
 
LinkBack Outils de la discussion
Vieux 24/08/2006, 15h11   #1
pedromalves@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sed Find and Replace Multiple Files

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
shift
SUBSTITUTION=$1
shift
for i;
do
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?

Thanks in advance,
Pedro Alves

  Réponse avec citation
Vieux 24/08/2006, 16h08   #2
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed Find and Replace Multiple Files

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"
> shift
> SUBSTITUTION=$1

SUBSTITUTION="$1"
> 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;

  Réponse avec citation
Vieux 24/08/2006, 16h16   #3
Xicheng Jia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed Find and Replace Multiple Files

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


If you are going to pick arbitrary PATTERN/SUBSTITION, better escape
the delimiter which will be used in the later sed command, i.e. the
slash in the following example:

PATTERN=$(printf "%s" $1 | sed 's#/#\\/#g')

similar to the other variable.

> shift
> SUBSTITUTION=$1
> shift
> for i;
> do


This 'for' loop looks problematic, i.e. where did you increment the
value of i? how about change this structure to be like:

while [ $# -ne 0 ]
do
#do stuff on $1
shift
done

> mv $i $i.old;
> sed -e 's/$PATTERN/$SUBSTITUTION/g' $i.old > $i;


change your sed line to be enclosed by double-quotes:

sed -e "s/$PATTERN/$SUBSTITUTION/g" $i.old > $i;

Xicheng

  Réponse avec citation
Vieux 24/08/2006, 21h16   #4
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed Find and Replace Multiple Files

On 24 Aug 2006 08:16:38 -0700, Xicheng Jia
<xicheng@gmail.com> wrote:
>> for i;
>> do

>
> This 'for' loop looks problematic, i.e. where did you increment the
> value of i?
>

'for i' is equivalent to 'for i in "$@"'


--
"Nominal fee". What an ugly sentence. It's one of those things that
implies that if you have to ask, you can't afford it.
-- Linus Torvalds
  Réponse avec citation
Vieux 25/08/2006, 00h09   #5
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sed Find and Replace Multiple Files

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18129 seconds with 13 queries