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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With