Author |
Message |
AvA #1 / 10
|
 Filehandel
I am trying to replace every "y" with a "n" in all files in a given directory. What I have sofar is: $dir = "/home'; opendir (DIR, $dir); while (defined($file = readdir(DIR))){ open(FILE, "+<$file"); foreach($line =<FILE>){ $line =~ s/y/n/g; close FILE; } Quote: }
But this obviously isn't working. Could someone give me an example on how to do it. Thanks.
|
Mon, 17 May 2004 18:46:42 GMT |
|
 |
John J. Tramme #2 / 10
|
 Filehandel
Quote:
> I am trying to replace every "y" with a "n" in all files in a given > directory.
[snip] perl -pi -e 'tr/y/n/' -- Configuracion de clasificacion: {*filter*}o-fantastico, GOL! Busca el lubrificante!
|
Mon, 17 May 2004 18:52:59 GMT |
|
 |
John J. Tramme #3 / 10
|
 Filehandel
Quote:
> > I am trying to replace every "y" with a "n" in all files in a given > > directory. > [snip] > perl -pi -e 'tr/y/n/'
Whoops. perl -pi -e 'tr/y/n/' * -- [M]en become civilized, not in proportion to their willingness to believe, but in proportion to their willingness to doubt. - H.L. Mencken
|
Mon, 17 May 2004 19:10:18 GMT |
|
 |
AvA #4 / 10
|
 Filehandel
Quote:
> Whoops. > perl -pi -e 'tr/y/n/' *
Thanks John, I knew about that option, but I need a script version. The example was just to simplify things. The whole script is larger, so this doesn't fit my needs.
|
Mon, 17 May 2004 19:31:01 GMT |
|
 |
Steve Grazzin #5 / 10
|
 Filehandel
Quote:
> > Whoops. > > perl -pi -e 'tr/y/n/' * > Thanks John, > I knew about that option, but I need a script version. > The example was just to simplify things. > The whole script is larger, so this doesn't fit my needs.
Don't know if this will fit your needs either, but here is the 'official' script version of the 'pi' switches from the perlrun manpage. #!/usr/bin/perl $extension = '.orig'; LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT); hth- Steve
|
Mon, 17 May 2004 20:53:28 GMT |
|
 |
Jürgen Exne #6 / 10
|
 Filehandel
Quote:
> I am trying to replace every "y" with a "n" in all files in a given > directory. > What I have sofar is: > $dir = "/home'; > opendir (DIR, $dir); > while (defined($file = readdir(DIR))){ > open(FILE, "+<$file"); > foreach($line =<FILE>){ > $line =~ s/y/n/g; > close FILE; > } > } > But this obviously isn't working.
I can't see where you are writing the changed string back to the file. All you are doing is reading the string, changing the string, and then throwing it away and reading the next string. jue
|
Mon, 17 May 2004 21:02:55 GMT |
|
 |
John J. Tramme #7 / 10
|
 Filehandel
Quote:
> > Whoops. > > perl -pi -e 'tr/y/n/' * > Thanks John, > I knew about that option, but I need a script version. > The example was just to simplify things. > The whole script is larger, so this doesn't fit my needs.
OK, how about: #!/usr/bin/perl -w use strict; system(qq[ perl -pi -e 'tr/y/n/' * ]); Just kidding. :^) -- I would like to see an anime where Love conquers all, then goes mad with the power and sets up a repressive totalitarian regime that ruthlessly crushes and oppresses all non-love. -- Frank Raymond Michaels
|
Mon, 17 May 2004 21:04:18 GMT |
|
 |
Bart Lateu #8 / 10
|
 Filehandel
Quote:
>I am trying to replace every "y" with a "n" in all files in a given >directory.
option to replace the original file.Don't forget to print your modified data! With the -p option, you even don't need the while(<>) { ... } and
p.s. I'm afraid I haven't tested this. There could be some restrictions to what works and what not. -- Bart.
|
Mon, 17 May 2004 23:49:11 GMT |
|
 |
Dave V #9 / 10
|
 Filehandel
Quote:
> I am trying to replace every "y" with a "n" in all files in a given > directory. > What I have sofar is:
An uncompilable code snippet. Notice the following errors in what you *do* have: (Omitting standard suggestions to enable warnings, check for success on file open, etc.) Quote: ^ ^ These don't match -- unterminated string. Quote: > opendir (DIR, $dir); > while (defined($file = readdir(DIR))){ > open(FILE, "+<$file");
Be careful. Are you sure you want to deal with '.' and '..'? Quote: > foreach($line =<FILE>){
[ line indentation for clarity ] > $line =~ s/y/n/g; > close FILE; You have closed the file after processing just one line. Probably not what you intended. Quote: > } > } > But this obviously isn't working.
No, and changes won't be saved anywhere, either. Quote: > Could someone give me an example on how to do it.
Several good method have been suggested in other posts. Quote:
|
Mon, 17 May 2004 22:31:52 GMT |
|
 |
Benjamin Goldber #10 / 10
|
 Filehandel
Quote:
> I am trying to replace every "y" with a "n" in all files in a given > directory. > What I have sofar is: > $dir = "/home'; > opendir (DIR, $dir); > while (defined($file = readdir(DIR))){ > open(FILE, "+<$file"); > foreach($line =<FILE>){ > $line =~ s/y/n/g; > close FILE; > } > } > But this obviously isn't working. > Could someone give me an example on how to do it.
#!/usr/local/bin/perl -w use strict; use warnings; foreach my $file (do { opendir( local(*DIR), "/home" ); grep -f, readdir(DIR); Quote: } ) {
open( local(*FH), "+<$file" ) or warn $! and next; while( sysread(FH, local($_), 8192) ) { sysseek FH, -length, 1; tr/y/n/; print; } close FH or warn $!; Quote: }
[this code is untested] -- Klein bottle for rent - inquire within.
|
Fri, 21 May 2004 10:29:40 GMT |
|
|