Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Split a file based on column

by Kenosis (Priest)
on Jan 16, 2013 at 20:43 UTC ( #1013643=note: print w/replies, xml ) Need Help??


in reply to Split a file based on column

Although your data's a good candidate for a CSV parsing module, perhaps the following will assist:

use strict; use warnings; while (<>) { if ( my ($col2) = /^\d+\|(\d+?)\|/ ) { open my $fh, '>>', 'sample_1_' . $col2 . '.txt' or die $!; print $fh $_; } }

Usage: perl scriptName.pl sample_1.txt

The regex captures the value in the second column, and that's used later in creating the file name. The file's opened for appending, and the line's written to it. Since a lexically-scoped file handle is used, the file's automatically closed at the end of the if code block.

Replies are listed 'Best First'.
Re^2: Split a file based on column
by brad_nov (Novice) on Jan 16, 2013 at 22:45 UTC
    Thanks, got it working

      Excellent, brad_nov, and you're most welcome!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013643]
help
Chatterbox?
[SuicideJunkie]: So, possibly interesting thing; I had that script which was using more than all the ram to open an absurdly large spreadsheet. I set it up to spin overnight nightly, so it stopped releasing memory since it was never fully exited.
[SuicideJunkie]: Didn't expect that to be a problem. There's plenty of disk space for swap, and the memory space would be reused the next day. But apparently virtualbox dies with a guru meditation when there's no physical memory unused on start.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-04-26 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (491 votes). Check out past polls.