将列添加到分组列的计数的awk命令
我有一个数据集选项卡,如下所示:(file.txt) 我想添加一个新的C列来显示分组a和B的计数 期望输出:将列添加到分组列的计数的awk命令,awk,Awk,我有一个数据集选项卡,如下所示:(file.txt) 我想添加一个新的C列来显示分组a和B的计数 期望输出: A B C 1 111 2 1 111 2 1 112 1 1 113 4 1 113 4 1 113 4 1 113 4 2 113 3 2 113 3 2 113 3 我试过这个: awk 'BEGIN{ FS=OFS="\t" } NR==FNR{ if (FNR>1) a[$2]+=$3
A B C
1 111 2
1 111 2
1 112 1
1 113 4
1 113 4
1 113 4
1 113 4
2 113 3
2 113 3
2 113 3
我试过这个:
awk 'BEGIN{ FS=OFS="\t" }
NR==FNR{
if (FNR>1) a[$2]+=$3
next
}
{ $(NF+1)=(FNR==1 ? "C" : a[$2]) }
1
' file.txt file.txt > file2.txt
请您试一下下面的样品好吗
awk '
FNR==NR{
count[$1,$2]++
next
}
FNR==1{
print $0,"C"
next
}
{
print $0,count[$1,$2]
}
' Input_file Input_file
在上述代码中添加BEGIN{FS=OFS=“\t”}
,以防数据以制表符分隔
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when first time Input_file being read.
count[$1,$2]++ ##Creating count with index of 1st and 2nd field and increasing its count.
next ##next will skip further statements from here.
}
FNR==1{ ##Checking condition if this is 1st line then do following.
print $0,"C" ##Printing current line with C heading here.
next ##next will skip further statements from here.
}
{
print $0,count[$1,$2] ##Printing current line along with count with index of 1st and 2nd field.
}
' Input_file Input_file ##Mentioning Input_file(s) here.
OP尝试中的问题:OP添加了$3的值(虽然逻辑看起来正常),但输入文件中没有第三个字段,所以它不起作用。此外,OP使用索引为第二字段,但是根据OP的注释,它应该是第一和第二字段。< P>您可以考虑使用GNU DATAMASH,例如:
datamash -HW groupby 1,2 count 1 < file.txt | column -t
谢谢你的帮助。有一个问题。我想用A列和B列进行分组。您的解决方案只计算B列。A和B在计数时必须相同。谢谢。@ersan,好的,当然,我现在已经编辑了解决方案。
datamash -HW groupby 1,2 count 1 < file.txt | column -t