A script to change "string1" to "string2" in 50 files.

A script to change "string1" to "string2" in 50 files.

Post by George J. Wakile » Tue, 24 Apr 2001 22:59:09



Hello,

I have a directory with 50 .c files.  I want to change "string1" in all
files to "string2".

Can anyone be kind to help me with a little script to do that?

I think one way is to write a "replace.sh" script.  Then sh replace.sh would
do it.

I know
cd dir
.
mv "string1" "string2"
.
but can't manage it all.

Thanks very much for your help.

George

 
 
 

A script to change "string1" to "string2" in 50 files.

Post by Jens.Toerr.. » Tue, 24 Apr 2001 23:14:21



Quote:> Hello,
> I have a directory with 50 .c files.  I want to change "string1" in all
> files to "string2".
> Can anyone be kind to help me with a little script to do that?
> I think one way is to write a "replace.sh" script.  Then sh replace.sh would
> do it.
> I know
> cd dir
> .
> mv "string1" "string2"
> .
> but can't manage it all.

No, you can't use mv to change strings within files, that's something
which sed is good for. So in your script you need something like

sed -e 's/string1/string2/g' input_file > outputfile
mv output_file input_file

(DO NOT send sed's output directly into input_file but to a temporary
file and rename it afterwards, or you will be very unhappy!)

                                     HTH, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

A script to change "string1" to "string2" in 50 files.

Post by Barry Margoli » Wed, 25 Apr 2001 03:55:03




Quote:>Hello,

>I have a directory with 50 .c files.  I want to change "string1" in all
>files to "string2".

>Can anyone be kind to help me with a little script to do that?

>I think one way is to write a "replace.sh" script.  Then sh replace.sh would
>do it.

>I know
>cd dir
>.
>mv "string1" "string2"
>.
>but can't manage it all.

I usually do this with Perl:

cd dir
perl -p -i.bak -e 's/string1/string2/' *

The original files will be renamed to filename.bak.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

A script to change "string1" to "string2" in 50 files.

Post by Eduardo Chapp » Wed, 25 Apr 2001 09:59:12



:) I have a directory with 50 .c files.  I want to change "string1" in all
:) files to "string2".
:)[snip]
:) mv "string1" "string2"

If string1 is in the files (as opposed to filenames), then this question
has been answered, if string1 is in the filenames you should use something
like:

ls -1 | grep string1 | sed 's/\(.*\)string1\(.*\)/mv \1string1\2 \1string2\2/'| sh

before you do this, however delete the last "| sh" of the command, and run
the command as it is, it should give you a list of commands of the form:

mv string1 string2

the |sh part will execute all of those rename commands, so make sure that
it will do its job correctly, before you execute the "|sh" part.

--
Eduardo
http://www.math.washington.edu/~chappa/pine/

 
 
 

A script to change "string1" to "string2" in 50 files.

Post by Trebo » Fri, 27 Apr 2001 10:27:56


You'll need a loop to make things easier. For example:

#!/bin/sh
STRING1="this is string 1"
STRING2="this is its replacement"
for FN in *.c
do
  echo Processing $FN
  sed "s/$STRING1/$STRING2/g" $FN > /tmp/tmp.$FN
  mv $FN $FN.orig
  mv /tmp/tmp.$FN $FN
done

HTH!
-Bob
 Andover, MA



Quote:> Hello,

> I have a directory with 50 .c files.  I want to change "string1" in all
> files to "string2".

> Can anyone be kind to help me with a little script to do that?

> I think one way is to write a "replace.sh" script.  Then sh replace.sh
would
> do it.

> I know
> cd dir
> .
> mv "string1" "string2"
> .
> but can't manage it all.

> Thanks very much for your help.

> George

 
 
 

A script to change "string1" to "string2" in 50 files.

Post by George J. Wakile » Sat, 28 Apr 2001 02:22:42


Hello,

Thank you all very much.  You were too kind.

There were different ways to do it and I benefited from that.

I got the replacement done.  That was great!

Kind regards,

George