在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Perl的优势:比C好写,比Shell高效,Linux普遍支持。 #!/usr/bin/perl -w # auth: [email protected] # what: split data_file by date. my $fullpath = $0; my $script_name = sprintf "%s", $fullpath =~ /[^\/]*\.pl/g; my $datafile; my $destpath; my $filename; my $ARGC = scalar @ARGV; if($ARGC<1 || $ARGC>2){ die "Usage: $script_name [data_file] [dest_path]\n"; }else{ $datafile = $ARGV[0]; $filename = sprintf "%s", $datafile =~ /[^\/]*$/g; if($ARGC == 2){ $destpath = $ARGV[1] . "/"; }else{ $destpath = sprintf "%s", $datafile =~ /([^\/]+\/)+/g; } } my %hash; open(DATA, $datafile) or die "Error: cannot open file $datafile\n"; while(<DATA>){ my $line = $_; my $date = substr($line, 0, 10); if(!$hash{$date}){ open($hash{$date}, ">>$destpath$filename.$date.tmp") or die "Error: cannot create file: $destpath$filename.$date.tmp\n"; } print {$hash{$date}} $line; } close(DATA); foreach my $fd (values(%hash)){ close($fd); }
|
请发表评论