in reply to Re: file phrase substitutation
in thread file phrase substitutation

First thanks for your reply. in case that all files are not resides under the same level this would not work correctly, I might use - find <dir> -name "*" -exec perl -pi -e's/this phrase/that locution/g' {} \; Azaria

Replies are listed 'Best First'.
Re^3: file phrase substitutation
by tirwhan (Abbot) on Nov 01, 2005 at 22:57 UTC

    Here's a solution that does it all in perl:

    #!/usr/bin/perl use warnings; use strict; use File::Find; use Tie::File; find (\&replace,"/path/to/directory"); sub replace { my $cur_file=$File::Find::name; return if (!-f $cur_file || !-w $cur_file); tie my @file_content,'Tie::File',$cur_file or die "Couldn't tie file $cur_file"; for (@file_content) { s/this phrase/that locution/g; } }

    You may want to read the Tie::File perldoc regarding file locking and memory consumption.

    And just for completeness, here's a solution that does it all without perl:

    find this/ -type f -exec sed -i 's/gg/dd/g' {} \;

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan