Using Raku (formerly known as Perl_6)
...summing values in a hash, returning all:
~$ raku -ne 'BEGIN my %h;
.split("|") andthen %h{.[0]} += .[2];
END .put for %h.sort;' file
Sample Input:
smiths|Login|2
olivert|Login|10
denniss|Payroll|100
smiths|Time|200
smiths|Logout|10
Sample Output (1):
denniss 100
olivert 10
smiths 212
Above is an answer written in Raku, a member of the Perl-family of programming languages. Raku is called at the command line with -ne non-autoprinting, linewise flags.
- We
BEGIN by declaring a %h hash.
- In the main loop, columns are split on
| pipe, and .[0] the first column is added as key to the %h hash, with .[2] the third column summed-in as value.
- At the
END, above the complete hash is sorted and returned.
So how to return the value for smiths, specifically? Simply substitute the final statement like so:
END .put for %h<smiths>;
Sample Output (2): 212
https://docs.raku.org
https://raku.org