zilore mumba wrote:
> Thanks all who assisted, especially Bill for the rewriting part and
> others for the regex syntax.
> The script rewrites the files but nothing is replaced. When I escape the
> space as \s+ or \s it signals an error of unrecognized escape \s.
>
> Could it be the regex are written or is it impossible to do the required
> substitution?
You're not indicating much about what went wrong. I works for me, so
it would seem there is something different there like maybe a lowercase
H on the filename. Run with debug on and see if it compares to my output.
Try this version - changes marked:
use strict;
use warnings;
use POSIX;
use File::Path;
use File::Copy;
my $debug = 1;
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$/i; # can they be lowercase too ? added /i
print "Doing $file\n" if $debug;
my @lines = ();
open IN, "+<$Grib_dir/$file" or die "open '$file': $! ($^E)";
binmode IN;
while (<IN>) {
my $cnt = 0; # added subst count next 6 lines
print "$cnt: $_" if $debug;
s/nxny 5041 \d{4}/nxny 5041/g && ++$cnt;
s/ nx \d{4}/ nx 71/g && ++$cnt; # added leading space to
s/ ny 1/ ny 71/g && ++$cnt; # avoid conflict with nxny
s/\(\d{4} x 1\)/\(71 x 71\)/g && ++$cnt;
print "$cnt: $_" if $debug;
push @lines, $_;
}
print "lines: @lines\n" if $debug;
seek IN, 0, SEEK_SET; # rewind file - reversed last 2 args
truncate IN, 0; # added in case we shorten file - truncate to 0
print IN @lines;
close IN;
}
closedir DIR;
__END__
Test data:
rec 1:0:date 2009072900 UGRD kpds5=33 kpds6=100 kpds7=850 levels=(3,82)
grid=255 850 mb 24hr fcst:
UGRD=u wind [m/s]
timerange 0 P1 24 P2 0 TimeU 1 nx 1380 ny 1 GDS grid 0 num_in_ave 0 missing 0
center 98 subcenter 0 process 110 Table 1
latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 1380
long -90.000000 to 0.000000 by 2.500000, (1380 x 1) scan 0 mode 128
bdsgrid 1
min/max data -21.0971 -5.09805 num bits 14 BDS_Ref -21.0971 DecScale 0
BinScale -10
latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 5041 1380
nxny 5041 1380 # test nxny 5041 \d{4}
nx 1380 # test nx \d{4}
ny 1 # test nx 1
(1380 x 1) # \(\d{4} x 1\)
end
Results of run with debug on:
Doing test.H
0: rec 1:0:date 2009072900 UGRD kpds5=33 kpds6=100 kpds7=850 levels=(3,82)
grid=255 850 mb 24hr fcst:
0: rec 1:0:date 2009072900 UGRD kpds5=33 kpds6=100 kpds7=850 levels=(3,82)
grid=255 850 mb 24hr fcst:
0: UGRD=u wind [m/s]
0: UGRD=u wind [m/s]
0: timerange 0 P1 24 P2 0 TimeU 1 nx 1380 ny 1 GDS grid 0 num_in_ave 0
missing 0
2: timerange 0 P1 24 P2 0 TimeU 1 nx 71 ny 71 GDS grid 0 num_in_ave 0
missing 0
0: center 98 subcenter 0 process 110 Table 1
0: center 98 subcenter 0 process 110 Table 1
0: latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 1380
0: latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 1380
0: long -90.000000 to 0.000000 by 2.500000, (1380 x 1) scan 0 mode
128 bdsgrid 1
1: long -90.000000 to 0.000000 by 2.500000, (71 x 71) scan 0 mode 128
bdsgrid 1
0: min/max data -21.0971 -5.09805 num bits 14 BDS_Ref -21.0971 DecScale 0
BinScale -10
0: min/max data -21.0971 -5.09805 num bits 14 BDS_Ref -21.0971 DecScale 0
BinScale -10
0: latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 5041 1380
1: latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 5041
0:
0:
0: nxny 5041 1380 # test nxny 5041 \d{4}
1: nxny 5041 # test nxny 5041 \d{4}
0: nx 1380 # test nx \d{4}
1: nx 71 # test nx \d{4}
0: ny 1 # test nx 1
1: ny 71 # test nx 1
0: (1380 x 1) # \(\d{4} x 1\)
1: (71 x 71) # \(\d{4} x 1\)
0:
0:
0: end
0: end
0:
0:
lines: rec 1:0:date 2009072900 UGRD kpds5=33 kpds6=100 kpds7=850 levels=(3,82)
grid=255 850 mb 24hr fcst:
UGRD=u wind [m/s]
timerange 0 P1 24 P2 0 TimeU 1 nx 71 ny 71 GDS grid 0 num_in_ave 0 missing 0
center 98 subcenter 0 process 110 Table 1
latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 1380
long -90.000000 to 0.000000 by 2.500000, (71 x 71) scan 0 mode 128
bdsgrid 1
min/max data -21.0971 -5.09805 num bits 14 BDS_Ref -21.0971 DecScale 0
BinScale -10
latlon: lat 90.000000 to 0.000000 by 2.500000 nxny 5041
nxny 5041 # test nxny 5041 \d{4}
nx 71 # test nx \d{4}
ny 71 # test nx 1
(71 x 71) # \(\d{4} x 1\)
end
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs