原创哈希数据导出算法

2016-08-29 13:28:19来源:oschina作者:清风_人点击

#Oracle客户端配置
ORACLE_HOME=/export/home/tmn/oracle/product/11.2.0/dbhome
NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"
export ORACLE_HOME NLS_LANG
PATH=$PATH:$ORACLE_HOME/bin
export PATH
LD_LIBRARY_PATH=/usr/lib64:/export/home/tmn/oracle/product/11.2.0/dbhome/lib32:/export/home/tmn/oracle/product/11.2.0/dbhome/lib:/usr/local/lib
export LD_LIBRARY_PATH
#对日期格式化处理
NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'
export NLS_DATE_FORMAT NLS_TIMESTAMP_FORMAT
alias echo='echo -e'
SHELL=/bin/bash
LC_ALL=zh_CN.GB18030
JAVA_JRE=/usr/jdk6/jre
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
LD_LIBRARY_PATH=/usr/lib64:/export/home/tmn/oracle/product/11.2.0/dbhome/lib32:/export/home/tmn/oracle/product/11.2.0/dbhome/lib:/usr/local/lib
PATH=.:/export/home/tmn/oracle/product/11.2.0/dbhome/bin:.:/export/home/tmn/oracle/product/11.2.0/dbhome/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/jdk6/bin:/usr/jdk6/jre/bin:/sbin:/usr/bin:.:/export/home/tmn/bin:/usr/jdk6/bin:/usr/jdk6/jre/bin:/sbin:/usr/bin:.
JAVA_HOME=/usr/jdk6
ODBCSYSINI=/usr/local/unixODBC/etc
ODBCINI=/usr/local/unixODBC/etc/odbc.ini
ODBCHOME=/usr/local/unixODBC
CLASSPATH=/usr/jdk6/lib:/usr/jdk6/jre/lib:.
ORACLE_HOME=/export/home/tmn/oracle/product/11.2.0/dbhome
export SHELL NLS_LANG LC_ALL JAVA_JRE LD_LIBRARY_PATH PATH JAVA_HOME ODBCSYSINI
export ODBCSYSINI ODBCINI ODBCHOME CLASSPATH ORACLE_HOME
#!/bin/perl
################################################################################
##      导出模板数据到excel
## 编写:清风
## 日期:2012/8/24 星期五 17:08:21 沈阳
## 用法:perl main.pl 用户名 密码 server 生成属性目录 数据字典位置 生成excel目录
################################################################################
#传入参数
my $username=shift;#用户名
my $password=shift;#密码
my $dbserver=shift;#server
my $attr_dir=shift;#生成属性目录
my $dict_dir=shift;#数据字典位置
my $excel_dir=shift; #生成excel目录
my $TEMPLATE_ID=shift; #模板ID
my $user=shift; #用户账号
my $condition=shift; #查询条件
my $mobile=shift; #手机号#系统参数
my $maxcpu=80;
my $maxmem=5000000;
#引用模块
use DBI;
use Shell;
use Encode;
use Encode qw(decode);
use POSIX qw(strftime); #格式化时间
use Data::Dumper;
use Getopt::Long;
use DBD::Oracle;
use Time::Local;
use Time::HiRes; #为了得到毫秒的数据
use Spreadsheet::WriteExcel;
use Excel::Writer::XLSX;
my $cpu =getcpu();
my $mem= getmem();
print "user:$cpu/n";
print "user:$mem/n";
if($cpu<$maxcpu && $mem<$maxmem) {
chomp($mobile);
if($mobile ne '')
{
$context="综合资源服务器负荷已经到达极限,请稍后再使用数据导出功能!";
my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');
my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");
my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");
my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");
$sth_mss->execute();
$sth_ora->execute();
$sth_mss->finish();
$sth_ora->finish();
$dbh_mss->disconnect();
$dbh_ora->disconnect();
}
#释放内存
print `free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free`;
exit 0;
}
#定义系统模板表和属性表
$MY_TEMPLATEDEF="A_TEMPLATEDEF";
$MY_RESATTRIBUTE="M_RESATTRIBUTE";
#查询条件
chomp($condition);
if($condition ne '1=1')
{
$condition="and city_id=".$condition;
} else {
$condition="and ".$condition;
}
#print "获取程序开始运行时间,以秒为单位计时..."."/n";
$START_TIME=time();
#print "连接数据库..."."/n";
$dbh = DBI->connect("DBI:Oracle:$dbserver","$username","$password") || die ('connecting failed!');
$dbh->{LongTruncOk} = true;
$dbh->{LongReadLen} = "10000";
#print "注意:连接的数据库是:$dbserver,用户名是:$username..."."/n";
#获得模型英文名
$sql_sta= "SELECT TPLNETYPE FROM A_TEMPLATEDEF T WHERE T.TPID = '$TEMPLATE_ID'";
$sql = $dbh->prepare($sql_sta);
$sql->execute();
my $RESCLASSENNAME = '';
while( my @sql_column = $sql->fetchrow_array() )
{
$RESCLASSENNAME=$sql_column[0];#表名
}
if($RESCLASSENNAME ne '')
{
if($TEMPLATE_ID ne '')
{
$RESCLASSENNAME=uc($RESCLASSENNAME);#转换成大写
$sql_sta= "SELECT DSTABLENAME FROM M_RESCLASS WHERE RESCLASSENNAME = '$RESCLASSENNAME'";
$sql = $dbh->prepare($sql_sta);
$sql->execute();
my $dstablename = '';
while( my @sql_column = $sql->fetchrow_array() )
{
$dstablename=$sql_column[0];#表名
}
my $sql_sta = "SELECT TPLCNNAME,TPLENNAME FROM ".$MY_TEMPLATEDEF." WHERE STATEFLAG=0 AND TPID =$TEMPLATE_ID";
my $filename = "";
$sql = $dbh->prepare($sql_sta);
$sql->execute();
while( my @sql_column = $sql->fetchrow_array() )
{
$filename=$sql_column[0];#模板中文名
#$filename=$sql_column[1];#模板英文名
}
#导出M_RESATTRIBUTE表数据到文件中以^_^分割
my $resattribute_ini=$attr_dir."/".$user."_".$RESCLASSENNAME."_resattribute.ini";
my $sqlcontenttmp="SELECT RESCLASSENNAME,ATTRIBUTECOLUMNNAME,ATTRIBUTEENNAME,ATTRIBUTECNNAME,IFCHANGE,INPUTTYPE,SOURCEEXPRESSION,DATATYPE FROM ".$MY_RESATTRIBUTE." WHERE RESCLASSENNAME='$RESCLASSENNAME'";
export_data($dbserver,$username,$password,$sqlcontenttmp,$resattribute_ini);
#导出系统模版表数据到文件中以^_^分割
my $templatedef_ini=$attr_dir."/".$user."_".$RESCLASSENNAME."_templatedef.ini";
$sqlcontenttmp="SELECT A.TPLNETYPE, B.RESCLASSCNNAME, A.TPLCOLDESC FROM ".$MY_TEMPLATEDEF." A, M_RESCLASS B WHERE A.TPLNETYPE = B.RESCLASSENNAME AND A.TPID = '$TEMPLATE_ID'";
export_data($dbserver,$username,$password,$sqlcontenttmp,$templatedef_ini);
#导出字典关联翻译属性到文件中以^_^分割
#select int_id,zh_label from RMS_EQUIPROOM where stateflag=0 中 int_id,zh_label 不可以有空格
open(RFILE1,"<$resattribute_ini") || die "1.Can not open $resattribute_ini : $!/n";
while(defined($line=))
{
chomp($line);
@field=split(//^/_/^/,$line);
$attributecolumnname=$field[1];
$attributeid=$field[2];
$inputtype=$field[5];
$sourceexpression=$field[6];
$datatype=$field[7];
if($inputtype eq 'select' || $inputtype eq 'pageselectmore')
{
#获得sourceexpression的表名
@fieldes=split(//s+/,$sourceexpression);
@id_name=split(/,/,uc($fieldes[1]));
$dict_table=$fieldes[3];
#获得sourceexpression的value名
$id_name=$id_name[0];
if($dict_table ne 'g_com_dict' & $datatype eq 'integer')
{
$sourceexpression=~s/$dict_table/$dict_table a/;
$sourceexpression=$sourceexpression.' and exists(select 1 from '.$dstablename.' b where stateflag=0 and b.'.$attributecolumnname.' = a.'.$id_name.' )';
}
my $dict_file=$dict_dir."/".$user."_".$RESCLASSENNAME."_".$attributecolumnname.".ini";#字典导出文件名
$sqlcontenttmp=$sourceexpression;
open(RFILE2,"<$templatedef_ini") || die "Can not open $templatedef_ini : $!/n";
while(defined($line=))
{
# TPLNETYPE TPLCNNAMETPLCOLDESC
#---------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#JTWIND_CON 系统默认查询int_id,city_id,is_wrong,wrong_info,zh_label,related_equiproom,equ_type,equ_subclass,ne_code,asset_no,model,vendor,brand,supplier,company_maintain,start_time,end_time,status,fan_num,output_cap,maintain_person,flowid,modify_by,modify_date,stateflag,time_stamp@field=split(//^/_/^/,$line);
$tplcoldesc=$field[2];
@col_list=split(//,/,$tplcoldesc);
for(my $i=0;$i<@col_list;$i++)
{
my $col_name=trim($col_list[$i]);
if($col_name eq $attributeid)
{
export_data($dbserver,$username,$password,$sqlcontenttmp,$dict_file);
}
}
}
}
} print "hash_join($dict_dir,$excel_dir,$resattribute_ini,$templatedef_ini,$filename,$dbserver,$username,$password,$dstablename,$mobile,$condition)/n";
hash_join($dict_dir,$excel_dir,$resattribute_ini,$templatedef_ini,$filename,$dbserver,$username,$password,$dstablename,$mobile,$condition);
#print "导出数据用时".reckon_runtime($START_TIME)."秒/n";
}
}
#!/bin/perl
################################################################################
##      生成导出模板数据的SQL语句,将数据导出到excel
## 编写:清风
## 日期:2012/8/24 星期五 18:09:23 沈阳
##
################################################################################
sub hash_join
{
my $dict_dir=shift;
my $excel_dir=shift;
my $attr_file=shift;
my $tmp_file=shift;
my $filename=shift;
my $dbserver=shift;
my $username=shift;
my $password=shift;
my $dstablename=shift;
my $mobile=shift;
my $condition=shift;
my %col_id_hash=();#id对应的字段
my %col_zhname_hash=(); #id对应的字段中文名
my @need_decode_array=(); #存放需要翻译的字段
my %col_inputtype_hash=();#INPUTTYPE哈希数据为0的不处理,为1的用函数idname()
my $resclassenname;
if (!$excel_dir || !$dstablename ||!$attr_file || !$tmp_file || !$dict_dir || !$dbserver|| !$username || !$password)
{
die "Usage: perl 参数不正确!/n";
}
open(RFILE1,"<$attr_file") || die "2.Can not open $attr_file : $!/n";
while(defined($line=))
{
#RESCLASSENNAMEATTRIBUTECOLUMNNAMEATTRIBUTEENNAME ATTRIBUTECNNAMEIFCHANGEINPUTTYPESOURCEEXPRESSION
#------------------------------------------------ ------------------------------------------------
#IPACCESS related_server_idrelated_server_id AR(或BR/AR)承载业务局 1pageselectmoreselect int_id,zh_label from V_RMS_IPACCESS where stateflag=0
#IPACCESS capacity capacity 承载业务容量(单位:万用户)0input
#IPACCESS con_mss_namecon_mss_name 对接的CE设备名称 2select select a.value,a.name from g_com_dict a,g_com_dict b where a.super_code=b.code and b.dict_index='COMM_MATCH' order by a.name
#IPACCESS con_mss_portcon_mss_port 对接的CE设备端口 1select select int_id,zh_label from RMS_GPRSPORT where stateflag=0

chomp($line);
@field=split(//^/_/^/,$line);
$resclassenname=$field[0];
$attributecolumnname=$field[1];
$resattributeid=$field[2];
$attributecnname=$field[3];
$ifchange=$field[4];
$inputtype=$field[5];
$sourceexpression=$field[6];
#模型字段和物理字段对应关系
$col_id_hash{$resattributeid}=${attributecolumnname};
#物理字段和字段名称对应关系
$col_zhname_hash{$resattributeid}=${attributecnname};
#INPUTTYPE输入类型,默认为0,pageselectmore为1
$col_inputtype_hash{$attributecolumnname}=0;
#对于翻译,字典翻译有2种,其他的都一样
if($ifchange eq 0)
{
next;#对于不需要翻译的不做处理
}
#没有字典表的字典翻译
if($ifchange eq 2 && $sourceexpression=~//;/ )
{
$dict_file=$dict_dir."/".$attributecolumnname.".ini";
%$attributecolumnname=();
@dict_list=split(//;/,$sourceexpression);
foreach(@dict_list)
{
if($_=~//,/)
{
@dict_data=split(//,/,$_);
${$attributecolumnname}{$dict_data[0]}=$dict_data[1];
}
else
{
${$attributecolumnname}{$_}=$_;
}
#print "$attributecolumnname ${$attributecolumnname} $dict_data[0] $dict_data[1]/n";
}
@need_decode_array=(@need_decode_array,$attributecolumnname);
next;
}
#关联翻译,有字典表的翻译同样处理。
if($ifchange ne 0 )
{
if($inputtype eq 'select'|| $inputtype eq 'pageselectmore')
{
$dict_file=$dict_dir."/".$user."_".$RESCLASSENNAME."_".$attributecolumnname.".ini";
%$attributecolumnname=();
if( -s $dict_file )#如果存在$dict_file则打开,如果不加判断可能会报错.
{
open(RDICT,"<$dict_file") || next "3.Can not open $dict_file : $!/n";
while(defined($dict_line=))
{
chomp($dict_line);
@dict_data=split(//^/_/^/,$dict_line);
${$attributecolumnname}{$dict_data[0]}=$dict_data[1];
}
@need_decode_array=(@need_decode_array,$attributecolumnname);
close(RDICT);
}
unlink ($dict_file);
}
}
}
close(RFILE1);
open(RFILE2,"<$tmp_file") || die "4.Can not open $tmp_file : $!/n";
while(defined($line=))
{# TPLNETYPE TPLCNNAMETPLCOLDESC
#---------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#JTWIND_CON 系统默认查询int_id,city_id,is_wrong,wrong_info,zh_label,related_equiproom,equ_type,equ_subclass,ne_code,asset_no,model,vendor,brand,supplier,company_maintain,start_time,end_time,status,fan_num,output_cap,maintain_person,flowid,modify_by,modify_date,stateflag,time_stamp
#MSSTRUNKGRP系统默认查询tkgp_direction,zh_label,city_id,status,rate,type,a_equiproom_id,source_ne_id,source_port_id,z_equiproom_id,remote_ne_id,remote_port_id,isunidirectional,time_stamp,stateflag,remark,creator,creat_time,modifier,modify_time @field=split(//^/_/^/,$line);
$tplnetype=$field[0];
$resclasscnname=$field[1];
$tplcoldesc=$field[2];
@col_list=split(//,/,$tplcoldesc);
@excel_tile=();
@excel_col=();
$unload_sql="select ";
for(my $i=0;$i<@col_list;$i++)
{
my $value=trim($col_list[$i]);
$col_name=$col_id_hash{$value};
$col_zhname=$col_zhname_hash{$value};
@excel_tile=(@excel_tile,$col_zhname);
@excel_col=(@excel_col,$col_name);
#回车换行导致导出文件不对,串行修改
#$unload_sql=$unload_sql."replace(replace(".$col_name.",chr(13),''),chr(10),''),"
$unload_sql=$unload_sql.$col_name.","
}
chop($unload_sql);
$unload_sql=$unload_sql." from ".$dstablename." where stateflag=0";
}
$unload_sql=$unload_sql.' '.$condition;
print "/n导出SQL语句:/n$unload_sql/n/n";
close(RFILE2);
#导出数据文件名
$file_name=$excel_dir.'/'.$user.'_'.$dstablename.'.ini';#导出模版数据
export_data($dbserver,$username,$password,$unload_sql,$file_name);
#print "/n";
#生成excel文件名
my $timeStr = strftime "%Y-%m-%d.%H.%M", localtime;
#$excel_file=$excel_dir.'/'.$user.'_'.$resclassenname.'.'.$timeStr.'.xlsx';
#my $url=$user.'_'.$resclassenname.'.'.$timeStr.'.xlsx';
$excel_file=$excel_dir.'/'.$user.'_'.$resclasscnname.'.'.$timeStr.'.xlsx';
my $url=$user.'_'.$resclasscnname.'.'.$timeStr.'.xlsx';
print "$url/n";
#如果导出数据文件不为空,则创建excel文件
if( -s $file_name )
{
my $inputDataFile=$file_name;
my $outputDataFile=$excel_file;
my @report_title=@excel_tile;
my @table_cols=@excel_col;
my @decode_cols=@need_decode_array;
my $max_row=900000; #设定生成EXCEL2007最大行数(90万),实际可以支持1048576
my $max_col=16384;#设定生成EXCEL2007最大列数(400),实际可以支持16384
my $all_row=0;
my $row=0;
my $sheet_num=1;
my $workBook = Excel::Writer::XLSX->new($outputDataFile);
#excel表头样式
my $format_header = $workBook->add_format(bold =>1, border => 1);
$format_header->set_font(decode('gbk','Tahoma'));
$format_header->set_size( 11 );
#excel body样式
my $format_body = $workBook->add_format(border => 1);
$format_body->set_font('Tahoma');
$format_body->set_size( 11 );
#添加模板数据sheet
$sheet_name=$workBook->add_worksheet(decode('gbk',$filename));
#excel表头
my $colNum=@report_title;
for my $col (0..$colNum-1)
{
$value=trim($report_title[$col]);
$sheet_name->write($row,$col,decode('gbk',$value), $format_header);
}
$row+=1;
#excel body
open (SOURCEDATAFILE, "<$inputDataFile");
while(defined($line=))
{
chomp($line);
my @cols=split(//^/_/^/,$line);
%col_ps=remenber_positon_by_id(@table_cols);
foreach(@decode_cols)
{
my $word=$_;
$data_ps=$col_ps{$_};#得到字段在数组中的位置
$decode_befor=$cols[$data_ps]; #得到相应字段的值
if($decode_befor =~ /^(.*/,.*)$/)
{
my @cols_sub=split(//,/,$decode_befor);
for $n (0..$#cols_sub)
{
$decode_befor_sub=$cols_sub[$n];
$new_data_sub=${$word}{$decode_befor_sub};
$cols_sub[$n]=$new_data_sub;
}
$cols[$data_ps]=join ',',@cols_sub;
}
else
{
if($data_ps ne '')
{
$new_data=${$_}{$decode_befor};
if($new_data ne '')
{
$cols[$data_ps]=$new_data;#赋给新的值
}
}
}
}
if($row<$max_row)
{
my $col=0;
while($col<$colNum)
{
if($col<$max_col)
{
$value=trim($cols[$col]);
$sheet_name->write($row, $col, decode('gbk',$value));
#$sheet_name->write($row, $col, decode('gb2312',$value), $format_body);
$col++;
}
else
{
#print "注意:超出设定最大列数:$max_col,后续字段不再导出!/n";
#print "注意:请调整程序配置最大列数!/n";
exit;
}
}
}
else
{
$sheet_num++;
$row=0;
#添加模板数据sheet
$sheet_name=$workBook->add_worksheet(decode('gbk',$filename.$sheet_num));
#excel表头
$colNum=@report_title;
for my $col (0..$colNum-1)
{
$value=trim($report_title[$col]);
$sheet_name->write($row,$col,decode('gbk',$value), $format_header);
}
$row++;
my $col=0;
while($col<$colNum)
{
if($col<$max_col)
{
$value=trim($cols[$col]);
$sheet_name->write($row, $col, decode('gbk',$value));
#$sheet_name->write($row, $col, decode('gb2312',$value), $format_body);
$col++;
}
else
{
#print "注意:超出设定最大列数:$max_col,后续字段不再导出!/n";
#print "注意:请调整程序配置最大列数!/n";
exit;
}
}
}
$row++;
$all_row++;
if($row%10000==0)
{
my $cpu =getcpu();
my $mem= getmem();
print "$cpu/n";
print "$mem/n";
print "$row/n";
if($cpu<$maxcpu && $mem<$maxmem)
{
chomp($mobile);
if($mobile ne '')
{
$context="综合资源服务器负荷已经到达极限,请稍后再使用数据导出功能!";
my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');
my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");
my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");
my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");
$sth_mss->execute();
$sth_ora->execute();
$sth_mss->finish();
$sth_ora->finish();
$dbh_mss->disconnect();
$dbh_ora->disconnect();
}
#释放内存
print `free && sync && echo 3 > /proc/sys/vm/drop_caches && echo "" && free`;
exit 0;
}
}
}
$row-=1;
$all_row-=1;
$workBook->close();
if ($row < 1)
{
unlink ($outputDataFile);
}
unlink ($file_name);
chomp($mobile);
if($mobile ne '')
{
$context="数据导出:文件[".$filename."]共导出".$all_row."行,耗时".reckon_runtime($START_TIME)."秒,请到[基础功能-数据导出]模块下载!";
my $dbh_mss=DBI->connect('dbi:ODBC:sms','SGSHRUser','SGSHRPwd');
my $dbh_ora=DBI->connect("DBI:Oracle:rmw","rmw2","rmw2!23lc");
my $sth_mss=$dbh_mss->prepare("exec sp_Messages_Send '$mobile','$context',0,0,0,'ZHZY',0,'','',''");
my $sth_ora=$dbh_ora->prepare("insert into app_sms (mobile, content, time_stamp, issend) values ('$mobile','$context',sysdate,'1')");
$sth_mss->execute();
$sth_ora->execute();
$sth_mss->finish();
$sth_ora->finish();
$dbh_mss->disconnect();
$dbh_ora->disconnect();
}
}
else
{
#print "查询数据库为空,删除文件$file_name.../n";
unlink ($file_name);
}
}
################################################################################
##      导出数据库数据到文件
## 编写:清风
## 日期:2012/8/23 星期四 13:26:51 沈阳
##
################################################################################
sub export_data
{
my $dbserver=shift; #数据库信息
my $username=shift;
my $password=shift;
my $sql=shift;#sql语句
my $file_name=shift;#执行sql后生成的文件名
my @field; #sql语句执行结果数组
#print "连接数据库.../n";
my $dbh = DBI->connect("DBI:Oracle:$dbserver","$username","$password");
if($dbh)
{
#print "连接数据库成功.../n";
}
else
{
#print "连接数据库失败.../n";
exit(1);
}
my $filename=`basename $file_name`;
#print "$file_name/n";
open(WFILE,">$file_name") || die "打开文件失败!/n";
#执行SELECT语句并获取数据
my $sth = $dbh->prepare("$sql");
$sth->execute();
#读取sql语句执行结果数据
while(@field=$sth->fetchrow_array)
{
foreach(@field)
{
$_=~s/[/r/n]+/ /g;#替换换行回车
}
write_file(WFILE,(@field));
print WFILE "/n";
}
close(WFILE);
$sth->finish();
#print "关闭数据库.../n";
$dbh->disconnect();
}
####################################################################################
## 函数名称:write_file
## 函数功能:将输入参数指定的数组写入文件中
## 输入参数:$WRFILE要写入数据的文件句柄
##@row_data 要写入文件中一行数据
## 输出参数:无
## 返回值: 无
## 说明:
####################################################################################
subwrite_file
{
my $i=0;
my ($W_FILE,@row_data)=@_;
my $col_num=@row_data;
for($i=0;$i<$col_num;$i++)
{
$row_data[$i]=trim($row_data[$i]);
#$mid_data=encode("gb2312",decode("gbk",$row_data[$i]));
print $W_FILE $row_data[$i].chr(94).chr(95).chr(94);
}
}
####################################################################################
# 函数名称:remenber_positon_by_id
# 函数功能:记住标题的位置,防止标题顺序改变和使数据异常
# 输入参数:@title 要分析的标题
#
# 输出参数:%positonlist 以标题为索引的数组
####################################################################################
sub remenber_positon_by_id
{
local(@title)=@_;
my $title_num = @title;
my %positonlist=();
for($i=0; $i<$title_num; $i++)
{
$mid_titel=$title[$i];
$positonlist{$mid_titel}=$i;
}
return (%positonlist);
}
####################################################################################
## 函数名称:trim
## 函数功能:将输入变量中的前后空格去掉。输入变量的值不变,返回去空格后的值
## 输入参数:$line 要去除前后空格的字符串变量
## 输出参数:无
## 返回值: 去掉前后空格后的字符串变量
## 说明:
####################################################################################
sub trim
{
my ($line)=@_;
$line=~s/^[ /t]+//;#替换掉开头空格
$line=~s/[ /t/r/n]+$//;#替换掉结尾空格
return $line;
}
####################################################################################
##      计算入参到调用该Perl程序时经过的时长,以S为单位
## 日期:2011.03.03北京
## 功能: 计算入参到调用该Perl程序时经过的时长,以S为单位
## 脚本说明:
## 入参1:起始时间
## 要求参数格式如下:2011-03-03 10:10:10
####################################################################################
sub reckon_runtime()
{
$start_time=shift;#起始时间
$cur_time=time();#当前时间
$result=0;
$result_sec=$cur_time-$start_time;

if($start_time ne '')
{
#如果时间超过1分钟,显示分钟
if($result_sec<=60)
{
$result=$result_sec;
}
elsif($result_sec>60)
{
$result_min = int($result_sec/60);
$result_sec = $result_sec%60;
$result = $result_min."分".$result_sec;
}
else
{
$result=$result_sec;
}
#如果时间超过60分钟,显示时钟
#如果时间超过24小时,显示天数
}
else{
$result="null";
}
}
####################################################################################
##      获取CPU、内存
## 编写:清风
## 日期:2015/12/29 13:03:54沈阳
####################################################################################
sub getcpu
{
@cpu=split(//n/,`top -b -n 2|grep %id`);
@cpu=split(/,/,$cpu[1]);
@cpu=split(//%/,$cpu[3]);
$cpu=trim($cpu[0]);
return $cpu;
}
sub getmem
{
@mem=split(/,/,`top -b -n 1|grep Mem`);
@mem=split(/k/,$mem[2]);
$mem=trim($mem[0]);
return $mem;
}
;

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台