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 > changing text in files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

changing text in files

Réponse
 
LinkBack Outils de la discussion
Vieux 08/12/2006, 13h41   #1
Stephan Grein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing text in files

Are you using a GNU sed? Then you can do this:

find . -type f -exec sed -i 's/Info\*/Info\&/g' {} \;

HTH,
--
Stephan Grein, <stephan at stephan minus rockt dot de>
https://stephan-rockt.de
GnuPG-Key-ID: 0xF8C275D4
FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4
  Réponse avec citation
Vieux 08/12/2006, 13h50   #2
Gary Wessle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut changing text in files


Hi
I need to replace all the occurrences of
Info*
with the string
Info&
in all the files in a given directory.

the command below which was thankfully supplied by some one here did
not do it. I tried to escape the "*" character but could not.

for file in *; do [ -f "$file" ] || continue; sed
's/Info*/Info&/g' "$file" > tempfile && cp -- tempfile "$file"; done

who can I do it?

thanks
  Réponse avec citation
Vieux 08/12/2006, 16h29   #3
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing text in files

Gary Wessle <phddas@yahoo.com> wrote:
>
> Hi
> I need to replace all the occurrences of
> Info*
> with the string
> Info&
> in all the files in a given directory.
>
> the command below which was thankfully supplied by some one here did
> not do it. I tried to escape the "*" character but could not.
>
> for file in *; do [ -f "$file" ] || continue; sed
> 's/Info*/Info&/g' "$file" > tempfile && cp -- tempfile "$file"; done


You need to escape both, the asterisk and the ampersand in sed's
substitute command:

for file in *; do
[ -f "$file" ] || continue
sed 's/Info\*/Info\&/g' "$file" >tempfile \
&& cp -- tempfile "$file"
done

--
Kenan Kalajdzic
  Réponse avec citation
Vieux 08/12/2006, 17h15   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing text in files

2006-12-09, 00:50(+11), Gary Wessle:
>
> Hi
> I need to replace all the occurrences of
> Info*
> with the string
> Info&
> in all the files in a given directory.
>
> the command below which was thankfully supplied by some one here did
> not do it. I tried to escape the "*" character but could not.
>
> for file in *; do [ -f "$file" ] || continue; sed
> 's/Info*/Info&/g' "$file" > tempfile && cp -- tempfile "$file"; done

[...]

Unluckily enough, the "*" happens to be special in the LHS (left
hand side) of a sed substitution (where it means 0 or more of
the preceding item) and "&" on the RHS (where it is replaced by
the matched portion).

So, that command replaces Inf with InfoInf and Infooo with
InfoInfooo for instance.

for file in *; do
[ -f "$file" ] &&
sed 's/Info\*/Info\&/g' < "$file" > tempfile &&
cp -- tempfile "$file"
done

(note that the "--" in the cp command line is only necessary for
the GNU version of cp, other implementations don't allow options
after arguments).


--
Stéphane
  Réponse avec citation
Vieux 09/12/2006, 09h18   #5
Michael Paoli
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: changing text in files

Gary Wessle wrote:
> I need to replace all the occurrences of
> Info*
> with the string
> Info&
> in all the files in a given directory.

find . -type f -exec sh -c '<<\! ed -- {}
1,$s/Info\*/Info\&/g
w
q' \;

  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 02h43.


É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,11288 seconds with 13 queries