Thanks Bill for your time and effort. Sorry the explanation of what am trying to do was not clear.
In the files I have fields which will appear as "nxny d1d1d1d1 d2d2d2d2" d1d1d1d1 and d2d2d2d2 are two digit numbers. d1d1d1d1. is always the same number (71x71=5041) and I want to keep, but d2d2d2d2. will vary from file to file, I want get rid of it. I want to keep "nxny d2d2d2d2" (n° of points in x and y directions) Later in the file I get "nxny d2d2d2d2", which I want to change to "nxny 5041" and "nx d2d2d2d2 ny 1" to change to "nx 71 ny 71" and "(d2d2d2d2 x 1)" to be changed to "(71 x 71)" Thanks in advance for your time Zilore --- On Thu, 8/20/09, Bill Luebkert <[email protected]> wrote: From: Bill Luebkert <[email protected]> Subject: Re: help rewrite files To: "zilore mumba" <[email protected]> Cc: [email protected] Date: Thursday, August 20, 2009, 10:48 PM zilore mumba wrote: > Once more Dear kind Perl users, I have a small problem, being so raw in perl. > I have binary files which contain some headers of the type nx 70 ny 1, and > nxny 3542 ny 1. > I want to replace all ocurrences of nx by 71, ny by 71 and nxny by 5041. > The code below does not give any error but I end up with files of size 0 and > the same zero size files are copied to current directory where the scrip is > located. > Help will be appreciated. I don't follow your replacement rules, but I fixed the rest and attempted your rules literally. I didn't attempt to change the basic logic. use strict; use warnings; use POSIX; use File::Path; use File::Copy; my $Grib_dir = 'grib_files'; opendir DIR, $Grib_dir or die "opendir failed on $Grib_dir: $! ($^E)"; while (my $file = readdir DIR) { next if -d $file; # Extra filtering, to copy only grib files ending in H next unless $file =~ /H$/; print "Doing $file\n" if $debug; my @lines = (); open IN, "+<$Grib_dir/$file" or die "open '$file': $! ($^E)"; binmode IN; while (<IN>) { # s/nxny {4}{ny 1}/{nxny 5041}/g; # ????? # s/nx d{4} {ny 1}/{nx 71 ny 71}/g; # ????? # your stated rules (probably more to it than you stated): # nxny => 5041 # nx => 71 # ny => 71 s/nxny/5041/g; s/(nx|ny)/71/g; push @lines, $_; } seek IN, SEEK_SET, 0; # rewind file print IN @lines; close IN; } closedir DIR; __END__
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
