如何使用unix以行方式打印特定列?

时间:2012-01-11 12:50:26

标签: unix awk grep nawk

我有一个输入文件,如下所示。

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

我想要输出如下:

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

将系列保持为series.i.e的常见打印值。关于结果code.i.e输入文件中的第三个(整数)或第四个(字符串)列的第一列。

例如:数据的第二列有n个系列;取9184200.该系列有4个setupresultcode(空,0,1,2)。每个结果代码的名称在第4列中给出。如果resultcode为0,我想打印;即successfulReleasedByService然后打印关于9184200系列的值11。

1 个答案:

答案 0 :(得分:1)

这样的东西可能会起作用,虽然我没有测试它,把它当作某种伪代码。

#!/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];
  }
}

这会产生类似

的东西
  

9184200,11,194,101

     

9184201,2,78,32

     

9184202,4,63,37