File Operations

Hi Folks,

Below is example of an Input data which is used, based on the last 2, 3 & 4 column, I want my first column data to be collated as shown in the output section.

a,ac,tc,ic
b,ac,tc,ic
c,ac,tc,ic
d,ac,tc,ic
b,bc,tc,ic
d,bc,tc,ic
e,bc,tc,ic

I want my output to be

a,b,c,d##ac,tc,ic
b,d,e##bc,tc,ic

Hi, try something like this:

awk -F, '
  {
    i=$2 FS $3 FS $4
    A=(i in A?A FS:"") $1
  }
  END {
    for(i in A)
      print A "##" i
  }
' file
2 Likes

Hello nikhil jain,

Could you please try following.

awk 'BEGIN{FS=OFS=",";s1="##"} FNR==NR{a[$2,$3,$4]=a[$2,$3,$4]?a[$2,$3,$4] FS $1:$1;next}  (($2,$3,$4) in a){print a[$2,$3,$4] s1 $2,$3,$4;delete a[$2,$3,$4]}'  Input_file  Input_file

OR

awk '
BEGIN{
   FS=OFS=","
   s1="##"
}
FNR==NR{
   a[$2,$3,$4]=(a[$2,$3,$4]?a[$2,$3,$4] FS:"")$1
   next
}
(($2,$3,$4) in a){
   print a[$2,$3,$4] s1 $2,$3,$4
   delete a[$2,$3,$4]
}'    Input_file  Input_file

Output will be as follows.

a,b,c,d##ac,tc,ic
b,d,e##bc,tc,ic

Thanks,
R. Singh

2 Likes