Created
March 8, 2026 09:31
-
-
Save troyp/7dd5c0401f5a062aec5c58dc7ba3fefe to your computer and use it in GitHub Desktop.
select and accumulate fields from a TSV file - copied from repo shlomif/cmd-line-spreadsheets-toolkit
This file contains hidden or 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 perl | |
| # Written by Shlomi Fish - http://www.shlomifish.org/ - 2016 | |
| # Licensed under the MIT/Expat License. | |
| # | |
| # Example: | |
| # cat my.tsv | accum-field -f ItersSum=0 -e '$N{ItersSum} += $F{Iterations}' | |
| use strict; | |
| use warnings; | |
| use Getopt::Long qw/ GetOptions /; | |
| use List::MoreUtils qw/firstidx/; | |
| my @fields; | |
| my $code = ''; | |
| GetOptions( 'fields|f=s' => \@fields, 'expr|e=s' => \$code ) | |
| or die('Error in command line arguments.'); | |
| our %N; | |
| my @new_names; | |
| foreach my $f (@fields) | |
| { | |
| my ( $name, $val ) = split /=/, $f, 2; | |
| push @new_names, $name; | |
| $N{$name} = $val; | |
| } | |
| my $header = <>; | |
| chomp $header; | |
| my @h = split /\t/, $header; | |
| my %titles = ( map { $h[$_] => $_ } keys @h ); | |
| sub print_l | |
| { | |
| my ($l) = @_; | |
| chomp $l; | |
| print $l, "\t", join( "\t", @N{@new_names} ), "\n"; | |
| return; | |
| } | |
| print $header, "\t", join( "\t", @new_names ), "\n"; | |
| our ( %F, @F ); | |
| while ( my $l = <> ) | |
| { | |
| chomp $l; | |
| @F = split( /\t/, $l ); | |
| %F = ( map { $h[$_] => $F[$_] } keys @F ); | |
| eval "no strict; no warnings; $code"; | |
| print_l($l); | |
| } |
This file contains hidden or 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 perl | |
| # Written by Shlomi Fish - http://www.shlomifish.org/ - 2016 | |
| # Licensed under the MIT/Expat License. | |
| # | |
| # Example: | |
| # cat my.tsv | select-fields -f Time -f Iterations -f Delta | |
| use strict; | |
| use warnings; | |
| use Getopt::Long qw/ GetOptions /; | |
| use List::MoreUtils qw/firstidx/; | |
| my @fields; | |
| GetOptions( 'fields|f=s' => \@fields ) | |
| or die('Error in command line arguments.'); | |
| my $header = <>; | |
| chomp $header; | |
| my @h = split /\t/, $header; | |
| my %titles = ( map { $h[$_] => $_ } keys @h ); | |
| my @indices; | |
| for my $f (@fields) | |
| { | |
| my $i = $titles{$f}; | |
| if ( !defined $i ) | |
| { | |
| die "Coult not find field $f in header!"; | |
| } | |
| push @indices, $i; | |
| } | |
| sub print_l | |
| { | |
| my ($l) = @_; | |
| chomp $l; | |
| print join( "\t", ( split /\t/, $l )[@indices] ), "\n"; | |
| return; | |
| } | |
| print_l($header); | |
| while ( my $l = <> ) | |
| { | |
| print_l($l); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment