Created
January 5, 2018 17:09
-
-
Save MattOates/0c54dd0152a5ca05b20f7dbd7b008591 to your computer and use it in GitHub Desktop.
Explode records of files into new files based on the value of a given field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
%*SUB-MAIN-OPTS<named-anywhere> = True; | |
my %out_files; | |
sub MAIN( @in-files where { .IO.f // die "Input file $_ not found" }, | |
Int :$f(:$field) = 0, | |
Str :$d(:$delimiter) = ',', | |
Str :$t(:$template) = '%') { | |
for @in-files -> $file { | |
my $io = $file.IO; | |
for $io.lines -> $line { | |
my @fields = $line.split($d); | |
my $file = $template.subst('%',@fields[$f]); | |
$file ~= $io.extension; | |
if not %out_files{$file}:exists { | |
%out_files{$file} = $file.IO.open(:w); | |
} | |
%out_files{$file}.print($line); | |
} | |
} | |
%out_files>>.close; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
===SORRY!===
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:7
------> Int :$f⏏(:$field) = 0,
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:8
------> Str :$d⏏(:$delimiter) = ',',
Shape declaration with () is reserved;
please use whitespace if you meant a subsignature for unpacking,
or use the :() form if you meant to add signature info to the function's type
at /Users/matt/bin/explode:9
------> Str :$t⏏(:$template) = '%') {
expecting any of:
shape declaration