LoginSignup
0
0

More than 1 year has passed since last update.

perlのワンライナーでreduceByKey

Last updated at Posted at 2022-01-17

Apache SparkのreduceByKey的な処理をbashで使いたくなることがよくある
毎回書くのは嫌なので、perlのコードをメモする。入力はTAB区切りでkey-valueの順。降順でTop20を出力:

perl -e 'my %h; while(<>) { my ($k, $v) = split("\t"); $h{$k} += $v; }; for my $k (@{[ sort { $h{$b} <=> $h{$a} } keys %h ]}[0..19]) { print join("\t", ($k, $h{$k})),"\n" }'
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0