2

I'm trying to concatenate strings in column 2 based on shared value in column 1. my file is like this:

2,John Doe
2,Roger Sterling
2,Blake Chaser
3,Rocky Horror
3,Jason Biggs
3,Bill Shakespeare

and I need this:

2,John Doe, Roger Sterling, Blake Chaser
3,Rocky Horror, Jason Biggs, Bill Shakespeare

I've tried a number of awk commands but I can't get it. Any help is appreciated

  • 1
    Can you show us what you've tried as [MCVE]s? – Kevin M Granger Jun 06 '17 at 18:46
  • 1
    duplicate of this https://stackoverflow.com/a/44380492/1435869 – karakfa Jun 06 '17 at 18:47
  • 2
    Possible duplicate of [In bash how to transform multimap to a map of ](https://stackoverflow.com/questions/44380249/in-bash-how-to-transform-multimapk-v-to-a-map-of-k-v1-v2) – James Brown Jun 06 '17 at 18:56
  • StackOverflow is about helping people fix their programming code. Don't tell us "I've tried a number of awk commands but I can't get it." SHOW US your code/output/error msgs;-) please ... Please read http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , http://stackoverflow.com/help/mcve and take the [tour](http://stackoverflow.com/tour) before posting more Qs here. Use the `{}` tool in the edit bar for proper formatting of code/data/err msgs. 'Run code snippet' doesn't apply here, does it ;-? ? Good luck. – shellter Jun 06 '17 at 19:11
  • Do you REALLY want a space after all but the first comma on every line? – Ed Morton Jun 06 '17 at 19:56

2 Answers2

1
awk -F "," '{arr[$1] = arr[$1] FS $2} END {for (i in arr) print i arr[i]}' file
Mago Buono
  • 23
  • 5
1

try: following will provide you output in same order of $1 in your Input_file.

awk -F, '!($1 in a){++i} {a[$1]=a[$1]?a[$1] OFS $NF:$1 OFS $NF;}{b[i]=a[$1]} END{;for(j=1;j<=i;j++){print b[j]}}' OFS=,   Input_file

EDIT1: Adding a non-one liner form of solution too here with a line by line detailed explanation too. I hope this helps you.

awk -F, '!($1 in a){                            ####checking here if $1 is not present in array a. If not then go to following block to execute statements.
        ++i                                     ####increment value of variable named i with 1 each time cursor comes into this block.
                   }
                   {
        a[$1]=a[$1]?a[$1] OFS $NF:$1 OFS $NF;   ####putting array a value and concatenating its value if that is already present using ? and : operators.
                   }
                   {
        b[i]=a[$1]                              ####creating an array b whose index is value of variable i and whose value is array a with current lines $1.
                   }
        END        {;
        for(j=1;j<=i;j++){                      ####starting a for loop now whose value will go till the value of variable i from 1.
        print b[j]                              ####printing the value of array b whose index is variable j.
                         }
                   }
        ' OFS=,  file117                        ####mentioning the value of output field separator as , and mentioning the Input_file name too here.
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93