On Mon, Jun 04, 2007 at 02:25:00PM +0200, Hannah Schroeter wrote:
> Hello!
> 
> On Mon, Jun 04, 2007 at 02:01:12PM +0200, Marc Espie wrote:
> >[...]
> 
> >Don't use for loops with find results, they do not scale well.
> >Also, beware of spaces in file.
> 
> >For this kind of thing, I generally use 'while read'
> 
> >find . -type f -name \*.htm -print|while read f; do sed s/old/new <"$f" 
> >>"$f.new"; done
> 
> This isn't safe wrt newlines in file names, either.

newlines in file names are a pretty infrequent occurrence.

In case this could happen, I would do it in perl and get rid of the sed 
entirely.

#! /usr/bin/perl
use File::Find;

sub
transform
{
        my $filename = shift;
        open my $fh, '<', $filename or 
                die "Problem with $filename: $!\n";
        open my $fh2, '>', "$filename.new" or
                die "Problem with $filename.new: $!\n";
        while (<$fh>) {
                s/old/new/;
                print $fh2 $_
        }
}

find(sub {
        return unless -f $_;
        return unless m/\.htm$/;
        transform($_);
}, '.');

or something like this...

Reply via email to