| Author |
replace the string in all the files in the directory
|
|
| qianz99@gmail.com 2006-11-29, 7:22 pm |
| Hi
I have a directory which has all my C files. Now I need to replace a
string.
Is that any unix command or script I can use so to replace the string
in all the files?
Thank you very much!
| |
| Pascal Bourguignon 2006-11-29, 7:22 pm |
| qianz99@gmail.com writes:
> I have a directory which has all my C files. Now I need to replace a
> string.
> Is that any unix command or script I can use so to replace the string
> in all the files?
for f in *.c ; do
cp ${f} ${f}~ && sed -e 's/string/replacement/g' < ${f}~ > ${f}
done
cp is used instead of mv, to keep the owner and access rights of ${f}.
&& is used to avoid overwritting the original file if the copy
didn't complete successfully.
Usually, you don't substitute strings, but tokens or words; read
man sed and man 7 regex and use \<word\> in the regexp!
--
__Pascal Bourguignon__ http://www.informatimago.com/
PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
| |
| Rafael Almeida 2006-11-30, 1:33 am |
| On Wed, 29 Nov 2006 23:43:43 +0100
Pascal Bourguignon <pjb@informatimago.com> wrote:
> for f in *.c ; do
> cp ${f} ${f}~ && sed -e 's/string/replacement/g' < ${f}~ > ${f}
> done
>
>
> cp is used instead of mv, to keep the owner and access rights of ${f}.
>
> && is used to avoid overwritting the original file if the copy
> didn't complete successfully.
>
> Usually, you don't substitute strings, but tokens or words; read
> man sed and man 7 regex and use \<word\> in the regexp!
I think he wanted to replace a string such as
"I'm a C literal string!"
to something else. So sed probably won't work for him, as a string may
span through several lines.
| |
| Logan Shaw 2006-11-30, 1:33 am |
| Pascal Bourguignon wrote:
> qianz99@gmail.com writes:
>
> for f in *.c ; do
> cp ${f} ${f}~ && sed -e 's/string/replacement/g' < ${f}~ > ${f}
> done
I suppose that would work, but this is a little more concise:
perl -p -i.bak -e 's/string/replacement/g' *.c
- Logan
| |
| raxitsheth2000@yahoo.co.in 2006-11-30, 7:27 am |
| few days ago i was also in same problem,
i have start using perl, seems to be easy,
any one knowing good start-resource for PERL ?
--raxit
qianz99@gmail.com wrote:
> Hi
>
> I have a directory which has all my C files. Now I need to replace a
> string.
> Is that any unix command or script I can use so to replace the string
> in all the files?
>
> Thank you very much!
| |
| Maurizio Loreti 2006-11-30, 7:27 am |
| Please, do not top-post.
raxitsheth2000@yahoo.co.in writes:
> any one knowing good start-resource for PERL ?
O'Reilly books...
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
| |
| John W. Krahn 2006-11-30, 1:17 pm |
| raxitsheth2000@yahoo.co.in wrote:
> few days ago i was also in same problem,
> i have start using perl, seems to be easy,
>
> any one knowing good start-resource for PERL ?
http://learn.perl.org/
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
|
|
|
|