#!/usr/bin/perl use strict; use warnings; use POSIX qw(strftime); use utf8; use open qw(:std :utf8); use Encode qw(encode decode); my $p_flag = 0; my $date = time; my $data_d = '/home/ghz/solar_power_wx/data'; my $out_dat_f = "$data_d/2-3_day.power"; my $out_dat_f_t = "$out_dat_f.tmp"; unlink $out_dat_f or printf "could not delete $out_dat_f\n"; if (-e $out_dat_f_t) { unlink $out_dat_f_t or printf "could not delete $out_dat_f_t\n"; } open(OUT, ">>", $out_dat_f) or die "omg! can't open output file: $out_dat_f"; open(OUT_t, ">>", $out_dat_f_t) or die "omg! can't open output file: $out_dat_f_t"; my $date_pat = strftime "%Y%m%d%H", localtime($date - (2 * 86400)); for (my $nday = 2; $nday >= 0; $nday--) { my $date_it = $date - ($nday * 86400); my $datey = strftime "%Y", localtime($date_it); my $tdatey = strftime "%Y%m%d", localtime($date_it); my $in_dat_f = "$data_d/$datey/solar_power.$tdatey"; if (-e $in_dat_f) { open(IN, "<", $in_dat_f) or die "omg! can't open input file: $in_dat_f"; while () { $p_flag = 1 if(/^$date_pat/); printf OUT; printf OUT_t if($p_flag); } close(IN); } } close(OUT); close(OUT_t); if (-e $out_dat_f_t && -s $out_dat_f_t >= 100000) { rename $out_dat_f_t, $out_dat_f; } elsif (-e $out_dat_f_t) { unlink $out_dat_f_t or printf "could not delete $out_dat_f_t\n"; }