Perl脚本常用操作
2017-08-10 14:17
387 查看
一、Perl脚本连接数据库
二、perl脚本去重(一个文件)
去除重复行,并记录每行出现的次数
注: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 = <FILE>))
{ $line=~s/[\r\n]$//;
chomp($line);
$hash{$line} +=1;
# print "$line,$hash{$line}\n";
}
while(defined (my $line = <FILE1>))
{ $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);
#! /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 = <FILE>)) { 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 = <FILE>)) { #从文件中取出要去重的数据 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 = <FILE>))
{ $line=~s/[\r\n]$//;
chomp($line);
$hash{$line} +=1;
# print "$line,$hash{$line}\n";
}
while(defined (my $line = <FILE1>))
{ $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);
相关文章推荐
- Python脚本控制的WebDriver 常用操作 <二十二> 处理alert / confirm / prompt
- Power Shell 操作 SharePoint 常用脚本 1
- Oracle常用操作脚本
- Python脚本控制的WebDriver 常用操作 <二> 关闭浏览器
- Python脚本控制的WebDriver 常用操作 <十四> 处理button dropdown 的定位
- MongoDB的mongo shell常用操作方法及操作脚本笔记
- MongoDB的mongo shell常用操作方法及操作脚本笔记
- SoapUI自动化--Groovy脚本常用操作
- Python脚本控制的WebDriver 常用操作 <十六> 处理对话框
- Python脚本控制的WebDriver 常用操作 <二十六> 上传文件
- Python脚本控制的WebDriver 常用操作 <三> 浏览器最大化
- 工作中常用的数据库操作脚本整理
- Python脚本控制的WebDriver 常用操作 <二十一> 执行JS
- Shell脚本文件中常用的操作语句
- Python脚本控制的WebDriver 常用操作 <五> 访问链接
- 常用的sql脚本 游标遍历操作
- Python脚本控制的WebDriver 常用操作 <十二> send_keys模拟按键输入
- 用perl操作mysql数据库的脚本
- 常用perl脚本工具
- 常用脚本语言Perl,Python,Ruby,Javascript一 Perl,Python,Ruby,Javascript