我正在为我编写代码以从日志中提取一些信息。 但每次我运行时,Perl都挂起,我的文本文件的大小不会增加a.k.a hang。
#!/usr/bin/perl
print "What are the month of logs you're looking to start?\n";
$opt1 = <>;
chomp($opt1);
print "What are the month of logs you're looking to end search?\n";
$opt2 = <>;
chomp($opt2);
print "What are the day of logs you're looking to start?\n";
$opt3 = <>;
chomp($opt3);
print "What are the day of logs you're looking to end search?\n";
$opt4 = <>;
chomp($opt4);
for($month=$opt1;$month<$opt2;$month++)
{
my $mymonth = sprintf '%02d', $month;
for($myDay=$opt3;$myDay<$opt4;$myDay++)
{
my $myDay2 = sprintf '%02d', $myDay;
#---------------------------------------------------------
# SET YESTERDAY DATE
# ----------------------
my $now = defined $_[0] ? $_[0] : time;
my $then = $now - 60*60*24*$myDay;
my $ndst = (localtime $now)[8] > 0;
my $tdst = (localtime $then)[8] > 0;
$then - ($tdst - $ndst) * 60 * 60 ;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($then);
my $yesterday ="16".$mymonth.$myDay2;
my $today =$yesterday;
print $today."\n";
my $count100 = 0;
my $count200 = 0;
my $count500 = 0;
print "\nCount total charging 200, 500 DN".$today.".log\n";
open (textfile,"C:\\VasBoss\\Log\\didi\\DN".$today.".log");
open (MYFILE, ">> 2TK.txt");
open (MYFILE2, ">> 5TK.txt");
open (MYFILE3, ">> 1TK.txt");
while (<textfile>)
{
($MobileNo,$SubscriberID,$Shortcode,$Telco,$MO_time,$MO_id,$TransactionType,$Language,$Content,$checkprice,$ChargeStatus,$Contentmsg,$ChatDisabled)= split(/,/);
($date,$MobileNo1) = split(/\>/,$MobileNo);
($MobileNo2,$MobileNo3) = split(/\=/,$MobileNo1);
($Content1,$Content2) = split(/\>/,$Content);
if(($checkprice =~ /200$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE $MobileNo3."\n";
$count200++;
}
if(($checkprice =~ /500$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE2 $MobileNo3."\n";
$count500++;
}
if(($checkprice =~ /100$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE3 $MobileNo3."\n";
$count100++;
}
}
print $count100;
print $count200;
print $count500;
sleep;
}
}
编辑,我找到了解决方案。 我发现全局显式变量与我的编译冲突。
答案 0 :(得分:2)
我认为你的问题是:
sleep;
因为来自documentation:
使脚本在(整数)EXPR秒内休眠,如果没有给出参数则永远休眠。
但是在我们的时候:
strict
和warnings
open
是更好的风格。 open
。答案 1 :(得分:2)
print $count100; <-- You think it hung before here,
print $count200;
print $count500;
sleep; <-- but it hung here as requested.
您没有看到print
语句的输出,因为STDOUT从行缓冲 [1] 开始,这意味着它只会将文本发送到{{1}当遇到换行符时。
要强制发送任何待处理的输出,请在print
之前添加以下内容。
sleep
但是你可能打算打印换行符,所以你应该添加的内容如下:
select()->flush();
请注意,没有参数的print "\n";
无限期地睡眠 [2] ,所以我不确定你为什么要使用它。
连接到终端时。它会以块缓冲方式开始。
或者,如果您已定义信号处理程序,则信号进入
答案 2 :(得分:-3)
这是解决方案。 我替换变量my count100 = 0;到count100 = 0;然后它工作了,它给出了我想要的结果
#!/usr/bin/perl
use warnings;
print "What are the month of logs you're looking to start?\n";
my $opt1 = <>;
chomp($opt1);
print "What are the month of logs you're looking to end search?\n";
my $opt2 = <>;
chomp($opt2);
print "What are the day of logs you're looking to start?\n";
my $opt3 = <>;
chomp($opt3);
print "What are the day of logs you're looking to end search?\n";
my $opt4 = <>;
chomp($opt4);
for($month=$opt1;$month<$opt2;$month++)
{
my $mymonth = sprintf '%02d', $month;
for($myDay=$opt3;$myDay<$opt4;$myDay++)
{
my $myDay2 = sprintf '%02d', $myDay;
#---------------------------------------------------------
# SET YESTERDAY DATE
# ----------------------
my $now = defined $_[0] ? $_[0] : time;
my $then = $now - 60*60*24*$myDay;
my $ndst = (localtime $now)[8] > 0;
my $tdst = (localtime $then)[8] > 0;
$then - ($tdst - $ndst) * 60 * 60 ;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($then);
my $yesterday ="16".$mymonth.$myDay2;
my $today =$yesterday;
print $today."\n";
$count100 = 0;
$count200 = 0;
$count500 = 0;
print "\nCount total charging 200, 500 DN".$today.".log\n";
open (textfile,"C:\\VasBoss\\Log\\didi\\DN".$today.".log");
open (MYFILE, ">> 2TK.txt");
open (MYFILE2, ">> 5TK.txt");
open (MYFILE3, ">> 1TK.txt");
while (<textfile>)
{
($MobileNo,$SubscriberID,$Shortcode,$Telco,$MO_time,$MO_id,$TransactionType,$Language,$Content,$checkprice,$ChargeStatus,$Contentmsg,$ChatDisabled)= split(/,/);
($date,$MobileNo1) = split(/\>/,$MobileNo);
($MobileNo2,$MobileNo3) = split(/\=/,$MobileNo1);
($Content1,$Content2) = split(/\>/,$Content);
if(($checkprice =~ /200$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE $MobileNo3."\n";
$count200++;
}
if(($checkprice =~ /500$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE2 $MobileNo3."\n";
$count500++;
}
if(($checkprice =~ /100$/i ) && ($Content2 =~ /=0$/i))
{
print MYFILE3 $MobileNo3."\n";
$count100++;
}
}
}
}
print $count100."\n";
print $count200."\n";
print $count500."\n";