How to print specific column in row wise using unix?

1.8k views Asked by At

I have one input file which is given below.

Values,series,setupresultcode,nameofresultcode,resultcode
2,9184200,,serviceSetupResultempty,2001
11,9184200,0,successfulReleasedByService,2001
194,9184200,1,successfulDisconnectedByCallingParty,2001
101,9184200,2,successfulDisconnectByCalledParty,2001
2,9184201,0,successfulReleasedByService,2001
78,9184201,1,successfulDisconnectedByCallingParty,2001
32,9184201,2,successfulDisconnectByCalledParty,2001
4,9184202,0,successfulReleasedByService,2001
63,9184202,1,successfulDisconnectedByCallingParty,2001
37,9184202,2,successfulDisconnectByCalledParty,2001

I want output as given below:

Series,successfulReleasedByService,successfulDisconnectedByCallingParty,successfulDisconnectByCalledParty,serviceSetupResultempty
9184200,11,194,101,2
9184202,4,63,37,

Keep series as common print value of series.i.e. first column with respect to result code.i.e third(integer) or fourth(string) column in input file.

For example: the second column of the data has n number of series; take 9184200. That series having 4 setupresultcode (empty,0,1,2). Name of each result code is given in 4th column. I want to print if resultcode is 0; i.e. successfulReleasedByService then print value 11 with respect to series 9184200.

1

There are 1 answers

2
Kristofer On

Something like this might work although I haven't tested it, regard it as some kind of pseudo code.

#!/bin/awk -f
BEGIN
{
  number_of_series=0;
}
{
  #This part will be executed for every line
  if ($3 =="0" || $3 == "1" || $3 == "2")
  {
    for (i=1; i<=number_of_series; i++)
    {
      #If the series has already been added
      if(seriesarray[i] == $2)
      {
        #Concat the results
        seriesarray[$2]=seriesarray[$2]","$1;
      }
      #If it's a new series
      else
      {
        number_of_series++;
        seriesarray[$2]=$1;
      }
    }
  }
}
END
{
  #Iterate over the series and print the series id and the concatenated results
  for (series in seriesarray)
  {
    print series, seriesarray[series];
  }
}

This would yield something like

9184200,11,194,101

9184201,2,78,32

9184202,4,63,37