Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge values for same key

Is that possible to use awk to values of same key into one row? For instance

a,100
b,200
a,131
a,102
b,203
b,301

Can I convert them to a file like this:

a,100,131,102
b,200,203,301
like image 461
Wang Nick Avatar asked Mar 10 '23 21:03

Wang Nick


2 Answers

If Perl is an option,

perl -F, -lane '$a{$F[0]} = "$a{$F[0]},$F[1]"; END{for $k (sort keys %a){print "$k$a{$k}"}}' file

These command-line options are used:

  • -n loop around each line of the input file
  • -l removes newlines before processing, and adds them back in afterwards
  • -a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace.
  • -e execute the perl code
  • -F autosplit modifier, in this case splits on ,

@F is the array of words in each line, indexed starting with $F[0]
$F[0] is the first element in @F (the key)
$F[1] is the second element in @F (the value)
%a is a hash which stores a string containing all matches of each key

like image 29
Chris Koknat Avatar answered Mar 15 '23 16:03

Chris Koknat


You can use awk like this:

awk -F, '{a[$1] = a[$1] FS $2} END{for (i in a) print i a[i]}' file

a,100,131,102
b,200,203,301

We use -F, to use comma as delimiter and use array a to keep aggregated value.

Reference: Effective AWK Programming

like image 116
anubhava Avatar answered Mar 15 '23 17:03

anubhava