as you already get sane and wise answers, I propose you a oneliner solution (change " to ' around the code to run it on Linux):
perl -lane "push @{$H{$F[0]}},$F[1]}{print map{$_,qq( ---> ),scalar @{ +$H{$_}},$/,(join $/,@{$H{$_}}),$/,$/}keys %H" data1.txt 0011 ---> 2 Sally Roy 1122 ---> 2 Brandon Simson 2233 ---> 1 George
You can use -MO=Deparse to have some clue on how to read it:
perl -MO=Deparse -lane "push @{$H{$F[0]}},$F[1]}{print map{ $_,qq( --- +> ),scalar @{$H{$_}},$/,(join $/,@{$H{$_}}),$/,$/ }keys %H" BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(' ', $_, 0); push @{$H{$F[0]};}, $F[1]; } { print map({$_, ' ---> ', scalar @{$H{$_};}, $/, join($/, @{$H{$_}; +}), $/, $/;} keys %H); } -e syntax OK
See perlrun for perl command line switches, but basically -l takes care of line ending (you are not chomp -ing lines!), -a does autosplit populating the @F array and -n wraps the code in a while loop without printing ( and -p does the same but printing). Braces in the part ..$F[1]}{print.. are a trick named Eskimo Greeting.
See perlvar for $/ and @F
You can explore these switches deparsing them one at time like in ( -e is for execute perl code, and -e 1 is just a null program):
L*perl -MO=Deparse -l -e 1 BEGIN { $/ = "\n"; $\ = "\n"; } '???'; -e syntax OK perl -MO=Deparse -a -e 1 LINE: while (defined($_ = readline ARGV)) { our @F = split(' ', $_, 0); '???'; } -e syntax OK perl -MO=Deparse -n -e 1 LINE: while (defined($_ = readline ARGV)) { '???'; } -e syntax OK perl -MO=Deparse -p -e 1 LINE: while (defined($_ = readline ARGV)) { '???'; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK
In reply to Re: Frequency occurence of same words in a file -- oneliner
by Discipulus
in thread Frequency occurence of same words in a file
by geek12
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |