perl脚本学习笔记
发布时间:2020-12-16 00:03:53 所属栏目:大数据 来源:网络整理
导读:#!/usr/bin/perl my $totaltimes=0;??//定义变量 my %tong_visit_times; //定义散列 my %tong_unique_users; my %userlist; my %tongidall; my $totalusers=0; my $totaltongs=0; my ($logfile)=@ARGV; //传递脚本的参数给变量 if(-e $logfile){ ????readlog
#!/usr/bin/perl
perl脚本学习
my $totaltimes=0;??//定义变量 my %tong_visit_times; //定义散列 my %tong_unique_users; my %userlist; my %tongidall; my $totalusers=0; my $totaltongs=0; my ($logfile)=@ARGV; //传递脚本的参数给变量 if(-e $logfile){ ????&readlogfile($logfile); //子程序调用 } #here read_tong &read_tongs(); $activeusers=(keys(%userlist));??//取散列的关键字的数量,keys在标量上下文中返回key的数量 $activetongs=(keys(%tong_visit_times));???????????????????????????????????????????????????????????????????????? print "总活动人次:".$totaltimes."n";?????????????????????????????????????????????????????????????????????????? print "活动用户数:".$activeusers."占总用户$totalusers的".(100*$activeusers/$totalusers)."%n";???????????????? print "涉及到桶数:".$activetongs."占总桶数$totaltongs的".(100*$activetongs/$totaltongs)."%n";???????????????? print "------------------------n";???????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????? open(USER,">last".$days."day.txt");???????????? //定义文件句柄USER,输出到last$daysday.txt?????????????????????????????????????????????????????????????? foreach $key(keys %userlist){?????????????????????????????????????????????????????????????????????????????????? ????print USER $key.":".$userlist{$key}."n";?????????????????????????????????????????????????????????????????? ????@i=split(/|/,$userlist{$key});???????????????????????????????????????????????????????????????????????????? ????if (defined($tong_unique_users{$i[0]})){?????????????????????????????????????????????????????????????????? ????????$tong_unique_users{$i[0]}=$tong_unique_users{$i[0]}+1;???????????????????????????????????????????????? ????}else{???????????????????????????????????????????????????????????????????????????????????????????????????? ????????$tong_unique_users{$i[0]}=1;?????????????????????????????????????????????????????????????????????????? ????}?????????????????????????????????????????????????????????????????????????????????????????????????????????? }?????????????????????????????????????????????????????????????????????????????????????????????????????????????? close(USER);?????? print "n桶号?? 活动人次????活动用户数??总数????活动比率n";?????????????????????????????????????????????????? foreach $key(sort(keys %tongidall)){?????????????????????????????????????????????????????????????????????????? ????if (defined($tong_unique_users{$key})){???????????????????????????????????????????????????????????????????? ???????? print $key."?? ".$tong_visit_times{$key}."???? ".$tong_unique_users{$key}."????????".$tongidall{$key}."????".(defined($ tongidall{$key})?100*$tong_unique_users{$key}/$tongidall{$key}."%":"此行数据有问题")."n"; ????}?????????????????????????????????????????????????????????????????????????????????????????????????????????? }?????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????? #--------------end of main program----------?? sub readlogfile{ ????my($curlogfile) =@_;??????????????????????????//@_表示缺省的参数 ????open(FILE,$curlogfile);?????????????????????? //打开文件句柄 ????while(<FILE>){ ??????????if ($_=~/UNI-LOG|/){?????????????????? //模式匹配UNI-LOG的行,计数器totaltimes加1 ????????$totaltimes++; ????????chop();?????????????????????????????????? //去除换行符号 ????????@items=split(/|/);?????????????????????? //以|进行分割,存入数组items中 ????????my $web=0; ????????my $pop3=0; ????????my $smtp=0; ????????my $visitmethod=$items[1];????????????????//通过数组下标取数组中的元素内容 ????????my $username=lc($items[2]); ????????my $remote_ip=$items[3]; ????????my $tongid=$items[4]; ????????my $visithistory=0; ????????if (defined($userlist{$username})){?????????????? //defined()如果为undef返回false,即变量为空的话返回假 ????????????$oldvalue=$userlist{$username}; ????????????@olditems=split(/|/,$oldvalue); ????????????$visithistory=$olditems[0]; ????????????$web=$olditems[1]; ????????????$pop3=$olditems[2]; ????????????$smtp=$olditems[3]; ????????} ????????if ($visitmethod eq "web"){ ????????????$web=1; ????????} ????????if ($visitmethod eq "pop3"){ ????????????$pop3=1; ????????} ????????if ($visitmethod eq "smtp"){ ????????????$smtp=1; ????????} ????????$visithistory="$web|$pop3|$smtp"; ????????$userlist{$username}=$tongid."|".$visithistory; ????????if (defined($tong_visit_times{$tongid})){ ????????????$tong_visit_times{$tongid}=$tong_visit_times{$tongid}+1; ????????}else{ ????????????$tong_visit_times{$tongid}=1; ????????} ??????} ????} } ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? sub read_tongs{???????????????????????????????????????????????????????????????????????????????????????????????????? ????????my $server="192.168.41.168";?????????????????????????????????????????????????????????????????????????????? ????????my $port=9901;???????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????use IO::Socket;??????????????????//使用模块?????????????????????????????????????????????????????????????????????????? ????????my $sk = IO::Socket::INET->new(PeerAddr => $server,???????? //使用方法,具体参考perldoc IO::Socket::INET或者[url]www.cpan.org??????????????????????????????????????????????[/url] ????????????????????????????????PeerPort => $port,???????????????????????????????????????????????????????????????? ????????????????????????????????Proto????=> 'tcp',???????????????????????????????????????????????????????????????? ????????????????????????????????timeout =>30);???????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????if (!($sk)) {?????????????????????????????????????????????????????????????????????????????????????????????? ????????????????print STDERR "fail to connect dbgate:".$server."n";?????????????????????????????????????????????? ????????exit;?????????????????????????????????????????????????????????????????????????????????????????????????????? ????????}?????????????????????????????????????????????????????????????????????????????????????????????????????????? ????print $sk "system.get serversn";????????????????????????//打印到文件句柄$sk?????????????????????????????????????????????????????? ????????$echo=<$sk>;???????????????????????????????????????? //从$sk中读取输出???????????????????????????????????????????????????? ????????chop($echo);?????????????????????????????????????????????????????????????????????????????????????????????? ????????if (substr($echo,1) eq "0"){???????????????????????????????????????????????????????????????????????????? ????????????????$echo=substr($echo,2);?????????????????????? //跳过开头的0?????????????????????????????????????????????????????? ????????@items=split/s/,$echo;?????????????????????????????????????????????????????????????????????????????????? ????????foreach $tongid(@items){?????????????????????????????????????????????????????????????????????????????????? ????????????print $sk "server.get ".$tongid." user_countn";?????????????????????????????????????????????????????? ????????????$echo=<$sk>;?????????????????????????????????????????????????????????????????????????????????????????? ????????????????????????my $tongcount=substr($echo,2);???????????????????????????????????????????????????????????? ????????????????????????$totalusers=$totalusers+$tongcount;???????????????????????????????????????????????????????? ????????????????????????$tongidall{$tongid}=$tongcount;???????????????????????????????????????????????????????????? ????????????????}?????????????????????????????????????????????????????????????????????????????????????????????????? ????????????????$totaltongs=(keys(%tongidall));???????????????????????????????????????????????????????????????????? ????????}?????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? }??
将看书学习的基础知识都用注释写在程序里面了
?
?
?
?
脚本用来处理一段log,log格式如下
?
Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: UNI-LOG|pop3|
wuyongguo123@sohu.com|210.51.1.80|34
Jul 30 00:00:02 192.168.95.126 LOG_UNILOG: UNI-LOG|pop3| wj1952@sohu.com|125.37.172.184|24 Jul 30 00:00:02 192.168.95.125 LOG_UNILOG: UNI-LOG|pop3| xgwang@sohu.com|116.15.32.105|37 Jul 30 00:00:02 192.168.95.126 LOG_UNILOG: UNI-LOG|pop3| sdg521837@sohu.com|159.226.8.165|37 Jul 30 00:00:02 10.10.71.21 LOG_UNILOG: UNI-LOG|pop3| zhouquanjun0196@sohu.com|59.40.168.63|43 Jul 30 00:00:02 10.10.71.19 LOG_UNILOG: UNI-LOG|pop3| michael_d@sohu.com|117.93.12.185|30 Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: UNI-LOG|pop3| wbg-79315@sohu.com|210.51.1.80|39 Jul 30 00:00:02 10.10.71.19 LOG_UNILOG: UNI-LOG|pop3| ww12872@sohu.com|218.206.212.236|50 Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: UNI-LOG|pop3| hans7281@sohu.com|210.51.1.80|46
?
?
脚本作用。统计活动用户数。活动的桶数。
?
统计结果如下
?
[@20.85 dailysummary]# /root/unilog_d.pl u.1
总活动人次:9 活动用户数:9占总用户96728766的9.30436763764773e-06% 涉及到桶数:8占总桶数89的8.98876404494382% ------------------------
桶号??? 活动人次??????? 活动用户数????? 总数??? 活动比率
24????? 1?????????????? 1?????????????? 1379631 ??????? 7.24831494798247e-05% 30????? 1?????????????? 1?????????????? 50160 ??????? 0.00199362041467305% 34????? 1?????????????? 1?????????????? 2761982 ??????? 3.62058840354499e-05% 37????? 2?????????????? 2?????????????? 91151 ??????? 0.00219416133668309% 39????? 1?????????????? 1?????????????? 680190 ??????? 0.000147017745041827% 43????? 1?????????????? 1?????????????? 3455293 ??????? 2.89411057180968e-05% 46????? 1?????????????? 1?????????????? 3838615 ??????? 2.60510626879747e-05% 50????? 1?????????????? 1?????????????? 1750166
?
本文出自 “风吹云动” 博客,请务必保留此出处http://www.voidcn.com/article/p-bygqkpbf-oc.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |