歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 簡單拆分文本腳本

簡單拆分文本腳本

日期:2017/3/1 15:53:56   编辑:關於Linux
簡單拆分文本腳本 今天被老師喊過來幫個小忙,有個文件太大了,把它分了。 在這裡留個備份吧。 01 #!/usr/bin/perl -w 02 use strict; 03 use warnings; 04 # 拆分文本文件 05 # 可以建個文件夾 06 # 目前是 csv 07 08 my $csv = 'xxx.csv'; 09 print "File read from is '$csv'.\n"; 10 my $in_fh = read_text($csv); 11 12 # 文件數目 13 my $num_of_files = 15; 14 15 # 行數 16 my $total_lines = 12465682; # 總行數 17 # 各文本大致行數 18 my $lines = int($total_lines / $num_of_files); 19 20 my $count = 0; 21 my $n = 1; 22 my $out_file = "xxx_$n.csv"; 23 print "Create $out_file ...\n"; 24 my $w_fh = write_text($out_file); 25 26 while (<$in_fh>) { 27 if ($n < $num_of_files and $count == $lines) { 28 $n++; 29 $count = 0; 30 close $w_fh; 31 $out_file = "xxx_$n.csv"; 32 print "Create $out_file ...\n"; 33 $w_fh = write_text($out_file); 34 } 35 print $w_fh $_; 36 $count++; 37 } 38 close $w_fh; 39 close $in_fh; 40 41 print "Done!\n"; 42 exit; 43 44 #==================================================== 45 # subroutions 46 47 # 讀取文本文件 48 sub read_text { 49 my $filename = shift; 50 my $fh; 51 open $fh, '<', $filename or die "Cannot open file '$filename': $!"; 52 return $fh; 53 } 54 55 # 寫文本文件 56 sub write_text { 57 my $filename = shift; 58 my $fh; 59 open $fh, '>', $filename or die "Cannot open file '$filename': $!"; 60 return $fh; 61 }
Copyright © Linux教程網 All Rights Reserved