split large csv file into multiple small files in perl -


I want to divide the CSV file into several small CSV files using Perl. My CSV file includes:

16047710472 4

12899376478 3

14034211 9 45 3

16132767680 4 < P> 17059884442 4

17808605446 3

15144433554 5

15145869428 4

12269245032 3 ... ...

I want to break this file into a smaller file using the line-number. And the output file will be in dynamic name. For example, the output file name will output1.csv and the second one will be output2.csv and output files on doing so.

Please give me some advice.

  Strictly use; Use warnings; Text: Use CSV; My $ file = 'csv_with_newline.csv'; My $ size = 3; My $ file_counter = 0; My $ line_counter = 0; My $ out; My $ csv = Text :: CSV_XS- & gt; New ({binary = & gt; 1, auto_diag = & gt; 1, sep_char => ';'}); "$: Encoding (UTF8)", open the $ file or "$ file: $!" Insert; While (my $ line = $ csv-> match ($ IN)) {$ line_counter ++; If ($ $ or $ line_counter & gt; $ shape) {if ($ out) {close close $ out; $ line_counter = 0; } $ File_counter ++; My $ outfile = "output $ file_counter.csv"; Open $ out, "& gt;: encoding (UTF8)", $ outfile or die "$ outfile: $!"; } $ Csv-> Print ($ out, line $); Print out $ "\ n"; } $ IN; About $ out;   

See article for detailed description

(Disclaimer: I've written the article)

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -