Simple split text script

Source: Internet
Author: User

The simple split text script was called by the teacher today to help me. A file is too large to be split. Leave a backup here. 01 #! /Usr/bin/perl-w02use strict; 03use warnings; 04 # split text file 05 # You can create folders 06 # csv07 08my $ csv = 'xxx.csv 'currently '; 09 print "File read from is '$ csv '. \ n "; 10my $ in_fh = read_text ($ csv); 11 12 # number of files 13my $ num_of_files = 15; 14 15 # number of lines 16my $ total_lines = 12465682; # Total number of lines 17 # Total number of lines of each text 18my $ lines = int ($ total_lines/$ num_of_files); 19 20my $ count = 0; 21my $ n = 1; 22my $ out_file = "xxx_rjn.csv"; 23 print "Create $ out_file... \ n "; 24my $ 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_20.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 # subroutions46 47 # Read the text file 48sub 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 # Write a text file 56sub write_text {57 my $ filename = shift; 58 my $ fh; 59 open $ fh, '> ', $ filename or die "Cannot open file '$ filename': $! "; 60 return $ fh; 61}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.