awk - 如果特定列匹配,则使用 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
解决方案
请您尝试以下操作。$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.
推荐阅读
- node.js - 如何在 kraken 交换中使用 node,js 维护有效的订单簿
- python - pytest-mock pathlib.Path.open
- r - 如果满足条件,则计数行
- arduino - Arduino 预处理器在定义术语 WIFI_AP 和 WIFI_STA 时行为不端
- vba - VBA 代码在 3 台机器中的 2 台上运行良好 - 不知道为什么
- sql - SQL:WHERE 子句中的 CASE 语句
- windows - Windbg ProcessUptime 不等于内核时间 + 用户时间
- java - 保护 Spring Boot 2 API
- java - Java VM 参数类型 -D 和 --
- netsuite - 有没有办法将联系人的类别与他们的角色联系起来?