Perl脚本常用操作

一、Perl脚本连接数据库

#! /usr/bin/perl
use URI::Escape;
use POSIX qw(strftime);
use DBI;
require "public.pl";
#$file=$ARGV[0];              #获取第一个输入数据
my $source_file = "read.log"; #读取文件
my $dest_file = "write.txt";  #写入文件
my $db = DBI->connect("DBI:mysql:database=mysql;host=127.0.0.1", "root", "", {'RaiseError' => 1});
open (FILE,"<$source_file") or die "Cannot open file $!\n";
open (SORTED,">$dest_file") or die "Cannot open file $!\n";
while(defined (my $line = ))
{
        chomp($line);         #读取行
        #@arr=split(/\s/,$line);
        #$app=$arr[0];
        #$tel=$arr[1];
        $log="log201706";
        $sql="select pid from $log.table where ... limit 1";
        print $sql."\n";
	my $rs = $db -> prepare($sql);
        $rs -> execute;
        if($rs->rows ne "0"){    
	        while(my $tmpRow = $rs->fetchrow_hashref()){
		   $p=$tmpRow->{'pid'};
		   print SORTED "$line\t$p\n";
	        }
        }else{
		print SORTED "$line\tNO PID\n";
	}            
}
close (FILE);
close (SORTED);

二、perl脚本去重(一个文件)

去除重复行,并记录每行出现的次数

#!/usr/bin/perl
use warnings;
use strict;
my %hash;
my $source_file=$ARGV[0];      #输入文件
my $dest_file = $ARGV[1];      #输出文件
open (FILE,"<$source_file") or die "Cannot open file $!\n";
open (SORTED,">$dest_file") or die "Cannot open file $!\n";
while(defined (my $line = ))
{
        #从文件中取出要去重的数据
        chomp($line);          #去除空格
        #$line=~ s/[\r\n]+//mg;#取出换行符
        $hash{$line} += 1;
}
foreach my $k (keys %hash) {
        print SORTED "$k\t$hash{$k}\n";   #改行打印出列和该列出现的次数到目标文件
}
close (FILE);
close (SORTED);

注:linux命令也可以实现此操作:sort test.txt | uniq -c

三、perl脚本去重(俩个文件)

获取俩个文件中的重复行,并记录重复次数

#!/usr/bin/perl
print 'hello';
use warnings;
use strict;
my %hash;
my $source_file = "num";   #去重文件1
my $source_file1 = "num1"; #去重文件2
my $dest_file = "num2";    #结果文件
open (FILE,"<$source_file") or die "Cannot open file $!\n";
open (FILE1,"<$source_file1") or die "Cannot open file $!\n";
open (SORTED,">$dest_file") or die "Cannot open file $!\n";
while(defined (my $line = ))
{      $line=~s/[\r\n]$//;
        chomp($line);
        $hash{$line} +=1;
        # print "$line,$hash{$line}\n";
}
while(defined (my $line = ))
{       $line=~s/[\r\n]$//;
        chomp($line);
        $hash{$line} +=1;
        # print "$line,$hash{$line}\n";
}
foreach my $k (keys %hash) {
if($hash{$k}!=1){
		print  "$k,$hash{$k}\n";
		print SORTED "$k\n";#改行打印出列和该列出现的次数到目标文件
	}
}
close (FILE1);
close (FILE);
close (SORTED); 

你可能感兴趣的:(Perl,perl,脚本)