Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Write to existing file with character insert

by Kenosis (Priest)
on Oct 24, 2012 at 19:19 UTC ( #1000695=note: print w/ replies, xml ) Need Help??


in reply to Write to existing file with character insert

Excellent suggestions have been provided. Given them, consider the following which which adds the use of File::Find::Rule to create the list of files for your substitution:

use strict; use warnings; use File::Slurp qw/ edit_file_lines /; use File::Find::Rule; my @files = File::Find::Rule->file() ->name(qr/^rc\.itm\d+$/) ->prune ->in('/etc'); edit_file_lines { s/(.*start ux)/#$1/ } $_ for @files;

prune prevents recursion. Note: Try the script on a 'scratch' directory first to verify your getting the desired results.

Hope this helps!


Comment on Re: Write to existing file with character insert
Select or Download Code
Re^2: Write to existing file with character insert
by Anonymous Monk on Oct 25, 2012 at 02:24 UTC

    prune prevents recursion.

    max_depth(1) also prevents recursion :)

    Based on the previous links I might write this which avoids the slurping of both files and filenames :)

    #!/usr/bin/perl -- use strict; use warnings; use File::Find::Rule; Main( @ARGV ); exit( 0 ); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; } sub pie(&@) { my( $cb, $file, $backup ) = @_; $backup ||= "$file~"; $backup = "$file$backup" if length $file > length $backup; rename $file, $backup or die Fudge( "backing up q{$file} to q{$bac +kup}:" ); open my $IN, "<", $backup or die Fudge( "reading q{$backup}:" ); open my $OUT, ">", $file or die Fudge( "writing $file:" ); local $_; while( defined( $_ = <$IN> ) ) { $cb->(); print $OUT $_; } } ## end sub pie(&@) #~ pie { s/^\s+// } "myfile.txt"; #~ pie { s/^\s+// } "myfile.txt", ".orig"; #~ pie { s/^\s+// } "myfile.txt", "myfile.txt.bak"; #~ pie { s/^\s+// } @ARGV; ## $ perltidy -csc -csci=10 -cscl="sub : BEGIN END" -otr -opr -ce -nib +c -i=4 -pt=0 "-nsak=*" #~ my @files = Path::Class::Rule->new->file->name( qr/^rc\.itm\d+$/ )- +>max_depth(1)->all( $dir ); #~ my @files = File::Find::Rule->new->file->name( qr/^rc\.itm\d+$/ )-> +max_depth(1)->in( $dir ); #~ my @files = find( file => name => qr/^rc\.itm\d+$/ => max_depth => +1 => in => $dir ); #~ for my $file( @files ){ #~ pie { #~ m/start ux/ and s{^}{#} #~ } $file; #~ } #~ for my $file( @files ){ #~ pie \&uxcomment, $file; #~ } #~ for my $file( @files ){ #~ eval { pie \&uxcomment, $file } or warn $@; #~ } sub uxcomment { s/^\s+// } sub Main { my $dir = \@_; my $filefinder = find( file => name => qr/^rc\.itm\d+$/, max_depth => 1, start => $dir, ); while( my $file = $filefinder->match ) { eval { pie \&uxcomment, $file } or warn $@; } } ## end sub Main

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2015-07-05 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls