help rewrite files
am 23.08.2009 14:25:03 von zilore mumba
--===============1255698045==
Content-Type: multipart/alternative; boundary="0-697250875-1251030303=:24483"
--0-697250875-1251030303=:24483
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
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 sp=
ace 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 su=
bstitution?
Help appreciated.
Zilore
use strict;
use warnings;
use POSIX;
use File::Path;
use File::Copy;
my $debug=3D1;
my $Grib_dir =3D 'grib_files';
opendir DIR, $Grib_dir or die "opendir failed on $Grib_dir: $! ($^E)";
while (my $file =3D readdir DIR) {
=A0 next if -d $file;
=A0 # Extra filtering, to copy only grib files ending in H
=A0 next unless $file =3D~ /H$/;
=A0 print "Doing $file\n" if $debug;
=A0 my @lines =3D ();
=A0 open IN, "+<$Grib_dir/$file" or die "open '$file': $! ($^E)";
=A0 binmode IN;
=A0 while () {
=A0 s/nxny 5041 \d{4}/nxny 5041/g;
=A0 s/nx \d{4}/nx 71/g;
=A0 s/ny 1/ny 71/g; =
=A0 s/\(\d{4} x 1\)/\(71 x 71\)/g;
=A0 push @lines, $_;
=A0 }
=A0 seek IN, SEEK_SET, 0; =A0 # rewind file
=A0 print IN @lines;
=A0 close IN;
}
closedir DIR;
__END__
=0A
--0-697250875-1251030303=:24483
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
|
top" style=3D"font: inherit;">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 ar=
e written or is it impossible to do the required substitution?
Help appr=
eciated.
Zilore
use strict;
use warnings;
use POSIX;
>use File::Path;
use File::Copy;
my $debug=3D1;
my $Grib_d=
ir =3D 'grib_files';
opendir DIR, $Grib_dir or die "opendir failed o=
n $Grib_dir: $! ($^E)";
while (my $file =3D readdir DIR) {
=
next if -d $file;
# Extra filtering,=
to copy only grib files ending in H
next unless $fil=
e =3D~ /H$/;
print "Doing $file\n" if
$debug;
my @lines =3D ();
open=
IN, "+<$Grib_dir/$file" or die "open '$file': $! ($^E)";
 =
; binmode IN;
while (<IN>) {
 =
; s/nxny 5041 \d{4}/nxny 5041/g;
&nb=
sp; s/nx \d{4}/nx 71/g;
=
s/ny 1/ny 71/g;  =
; &n=
bsp;  =
;
s/\(\d{4} x 1\)/\(71=
x 71\)/g;
push @lines, $=
_;
}
seek IN, SEEK_SET, 0; =
; # rewind file
p=
rint IN @lines;
close IN;
}
closedir
DIR;
__END__
--0-697250875-1251030303=:24483--
--===============1255698045==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1255698045==--
Re: help rewrite files
am 23.08.2009 21:40:48 von Bill Luebkert
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 () {
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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs