首页 > 解决方案 > 如果特定列匹配,则使用 BASH 将它们添加到同一行

问题描述

我的数据如下所示:

client1 5 10 12 17
client1 6 8 3 20
client1 3 2 2 2
client2 3 3 3 3 
client2 4 4 0 0
client2 0 3 3 9
...
client100 3 3 2 1
client100 1 1 1 2
client100 3 3 4 4 

我想让每个客户只有一行,每个客户行的所有信息都合并为一个。因此,例如,client1 和 client2 看起来像这样合并(但显然我需要合并所有客户端。)

client1 5 10 12 17 6 8 3 20 3 2 2 2
client2 3 3 3 3 4 4 0 0 0 3 3 9
awk '{ x[$1]=x[$1] " " $2; y[$2]=y[$2] " " $1; } 
END { 
   for (k in x) print k,x[k] >"OUTPUT1";  
   for (k in y) print k,y[k] >"OUTPUT2"; 
}' INPUT

标签: awk

解决方案


请您尝试以下操作。$1这应该以与Input_file 中发生的顺序相同的顺序提供输出。

awk '
{
  gsub(/\r/,"")
}
!a[$1]++{
  b[++count]=$1
}
{
  val=$1
  $1=""
  sub(/^ +/,"")
  c[val]=(c[val]?c[val] OFS:"")$0
}
END{
  for(i=1;i<=count;i++){
    print b[i],c[b[i]]
  }
}
' Input_file

说明:为上述代码添加详细说明。

awk '                                    ##Starting awk program.
{
  gsub(/\r/,"")
}
!a[$1]++{                                ##Checking condition if $1 is NOT in array a then do following.
  b[++count]=$1                          ##Creating array b with index count and value is $1.
}
{
  val=$1                                 ##Creating a variable val whose value is $1.
  $1=""                                  ##Nullifying $1 here.
  sub(/^ +/,"")                          ##Substituting initial space with null here.
  c[val]=(c[val]?c[val] OFS:"")$0        ##Creating an array c whose index is variable val and value is complete line value and its concatenating its own value each time cursor comes here.
}
END{
  for(i=1;i<=count;i++){                 ##Starting a for loop from i=1 till value of count here.
    print b[i],c[b[i]]                   ##Printing value of array b with index i and array c with index of b[i].
  }
}
' Input_file                             ##Mentioning Input_fie name here.

推荐阅读