search using perl one liner within a file [closed]

perl -alnwe '
    push @{$a{$F[0]}}, $F[1]; 
    }{ 
    for (keys %a) { print $_, "  ", join ",",@{$a{$_}} } ;'

Output:

cee  29,13
dee  123,56,30
bee  555

Explanation:

  • -a autosplit input line on whitespace
  • -l handle newlines
  • -n assume while(<>) loop around program
  • @F array comes from autosplit, $F[0] and $F[1] are the array elements.
  • %a is where we store results, first column as keys, second in the value as an array
  • }{ the eskimo operator, with the -n switch does the same as an END block

In the last section we just print the keys and values.

Leave a Comment