23 Nov, 2009, JohnnyStarr wrote in the 1st comment:
Votes: 0
Is there a *nix command that can copy / replace patterns in all files in a directory?
I imagine there could be a recursive command, but I haven't found any online.

An example would be something like:
%> replace send_to_char with sendch *.c

I imagine there might be some app I could get.
24 Nov, 2009, quixadhal wrote in the 2nd comment:
Votes: 0
Assuming your file names don't have spaces, this might work in bash. Please use it on a test copy first, this is off the top of my head and might not be quite right. :)

for i in *.c; do
mv $i $i.orig
sed 's/send_to_char/sendch/g' <$i.orig >$i
done
24 Nov, 2009, Lyanic wrote in the 3rd comment:
Votes: 0
Command line Perl FTW:

find . -type f | xargs perl -pi -e 's/Original/New/g'


You can also find/replace based on regexps with it.
24 Nov, 2009, Skol wrote in the 4th comment:
Votes: 0
Yeah, Perl's the only way I know of offhand, make backups ;).
IE:
perl -pi -e 's/send_to_char/sendch/g' *.c


Er, actually 'awk' can do it, but I believe it's a multi-step process?
24 Nov, 2009, JohnnyStarr wrote in the 5th comment:
Votes: 0
thanks all :cool:
24 Nov, 2009, Tyche wrote in the 6th comment:
Votes: 0
ruby -p -i.bak -e '$_.gsub!(/send_to_char/,"sendch")' *.[ch]
24 Nov, 2009, Runter wrote in the 7th comment:
Votes: 0
I'd probably do a perl or ruby command liner for it—as people already have given examples of.
24 Nov, 2009, JohnnyStarr wrote in the 8th comment:
Votes: 0
Tyche said:
ruby -p -i.bak -e '$_.gsub!(/send_to_char/,"sendch")' *.[ch]

gives me this:
$ ruby -p -i.bak -e '$_.gsub!(/send_to_char/,"sendch")' *.[ch]
-e:1: syntax error, unexpected tSTRING_BEG, expecting $end
$_.gsub!(/send_to_char/,"sendch")"act.comm.c

?
24 Nov, 2009, Runter wrote in the 9th comment:
Votes: 0
How about?
ruby -pe 'gsub(/send_to_char/, "sendch")' < *.c
24 Nov, 2009, JohnnyStarr wrote in the 10th comment:
Votes: 0
Runter said:
How about?
ruby -pe 'gsub(/send_to_char/, "sendch")' < *.c


$ ruby -pe 'gsub(/send_to_char/, "sendch")' < *.c
-bash: *.c: ambiguous redirect


EDIT: I am using Cygwin, so I don't know if that's causing conflicts.
24 Nov, 2009, Runter wrote in the 11th comment:
Votes: 0
Nah, probably something wrong with my syntax. I didn't test it. I used that for a single file replace in the past.
24 Nov, 2009, Scandum wrote in the 12th comment:
Votes: 0
Using TinTin++
#config {wordwrap} {off}

#substitute {send_to_char} {sendch}

#script {files} {ls *.c}

#foreach {$files[%*]} {file}
{
#buffer clear;
#scan $file;
#buffer write $file
}

In case anyone actually gives this a try, you'll need to grab the beta for the #buffer clear option to work, paste this code snippet to a file, and use tt++ <file>
24 Nov, 2009, Runter wrote in the 13th comment:
Votes: 0
How bout

for meh in *.c; do
ruby -pe 'gsub(/send_to_char/, "sendch")' < $meh
done
24 Nov, 2009, David Haley wrote in the 14th comment:
Votes: 0
ruby -pe 'gsub(/send_to_char/, "sendch")' < *.c

This – and other similar examples given above in Perl – won't work because you can't redirect several files at once into the program and expect it to know how to break them back out into files again. You need to process them one at a time. For example, with zsh,
for f in *.c ; do_something $f

Also be careful when redirecting stdin and stdout with the same file.

EDIT: Hmm, it seems that Runter already said something pretty similar.
24 Nov, 2009, kiasyn wrote in the 15th comment:
Votes: 0
open it up in your favourite IDE and do a search/replace :P
24 Nov, 2009, quixadhal wrote in the 16th comment:
Votes: 0
As kiasyn said….

vim *.c
qa:1,$s/send_to_char/sendch/g
:wn
a

Then if you had 20 files, 19@a to do the same search/replace on the rest of them.

Bam!
24 Nov, 2009, Lyanic wrote in the 17th comment:
Votes: 0
David Haley said:
ruby -pe 'gsub(/send_to_char/, "sendch")' < *.c

This – and other similar examples given above in Perl – won't work because you can't redirect several files at once into the program and expect it to know how to break them back out into files again. You need to process them one at a time. For example, with zsh,
for f in *.c ; do_something $f

Also be careful when redirecting stdin and stdout with the same file.

EDIT: Hmm, it seems that Runter already said something pretty similar.


The example I gave (see above) works perfectly - handles all files in the current directory. I use it all the time.
24 Nov, 2009, JohnnyStarr wrote in the 18th comment:
Votes: 0
@Lyanic
Yeah, it works great! Thanks.
24 Nov, 2009, David Haley wrote in the 19th comment:
Votes: 0
quixadhal said:
As kiasyn said….

vim *.c
qa:1,$s/send_to_char/sendch/g
:wn
a

Then if you had 20 files, 19@a to do the same search/replace on the rest of them.

Bam!

Or, just use :bufdo to do it in all buffers immediately.

vim *.c
:bufdo %s/send_to_char/sendch/g
:wa
:qa

No need to use macros in this instance.
24 Nov, 2009, Tyche wrote in the 20th comment:
Votes: 0
JohnnyStarr said:
Tyche said:
ruby -p -i.bak -e '$_.gsub!(/send_to_char/,"sendch")' *.[ch]

gives me this:
$ ruby -p -i.bak -e '$_.gsub!(/send_to_char/,"sendch")' *.[ch]
-e:1: syntax error, unexpected tSTRING_BEG, expecting $end
$_.gsub!(/send_to_char/,"sendch")"act.comm.c


I'm using cygwin ruby 1.8.7
?


So am I.
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
Works for me on Cygwin.

The error seems to indicate you left off a quote delimiter somewhere when you ran it.

P.S. It also work fine on these two versions running under Windows
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32]

P.P.S Are you using single quotes inside a string delimited by single quotes or double quotes inside a string delimited by double quotes? If so, then that's the problem. If you want or need to do that escape the inner quotes \' or \".
0.0/27