日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Perl脚本常用操作

發布時間:2023/12/9 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 = <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);

總結

以上是生活随笔為你收集整理的Perl脚本常用操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。