awkcontrol-m

how to merge rows ina csv file if if the first column is same in bash


I have written a program to find the open ports of a given IP. since the print function is in a loop my output format is like this:

IP1,22
IP1,23
IP1,24
IP2,25
IP2,26
IP3,27
IP3,30
IP3,45

How do I get it in this format:

IP1,22,23,24
IP2,25,26
IP3,27,30,45

EDIT: this is what I have done so far

awk'{a[$1]=(a[$1])? a[$1]r : r }

but I dont know how to progress forward from here.


Solution

  • Kindly always do add your efforts in your question in code tags. If you are not worried for order of output then try following.

    awk 'BEGIN{FS=OFS=","} {a[$1]=($1 in a ? a[$1] OFS : "") $2} END{for(i in a){print i,a[i]}}'  Input_file
    

    In case you need to get output in same order in which 1st field of Input_file is coming then try following.

    awk '
    BEGIN{
      FS=OFS=","
    }
    !b[$1]++{
      c[++count]=$1
    }
    {
      a[$1]=($1 in a ? a[$1] OFS : "") $2
    }
    END{
      for(i=1;i<=count;i++){
        print c[i],a[c[i]]
      }
    }'  Input_file
    

    Since later OP found out control-m characters were found in Input_file(s) too so adding following:

    tr -d '\r' < Input_file > temp && mv temp Input_file