awk计算列中出现的次数并在新列中打印

awk计算列中出现的次数并在新列中打印,awk,sed,grep,Awk,Sed,Grep,我有一个大文件,格式如下: NC_019859.2 Gnomon exon 58334 58504 . - . GeneID:110014915 NC_019859.2 Gnomon exon 54573 54723 . - . GeneID:110014915 NC_019859.2 Gnomon exon 52624 52680 . - . GeneID:110014915 NC_019859.2 Gnomo

我有一个大文件,格式如下:

NC_019859.2 Gnomon  exon    58334   58504   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    54573   54723   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    52624   52680   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    52413   52551   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    28715   28784   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    26768   26814   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    25856   25914   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    25374   25727   .   -   .   GeneID:110014915
NC_019859.2 Gnomon  exon    70772   70841   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    70672   70687   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    70494   70586   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    69020   69335   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    68831   68928   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    68251   68721   .   -   .   GeneID:110017276
NC_019859.2 Gnomon  exon    89665   89909   .   +   .   GeneID:110014398
NC_019859.2 Gnomon  exon    91117   91579   .   +   .   GeneID:110014398
NC_019859.2 Gnomon  exon    119534  120075  .   -   .   GeneID:101166461
NC_019859.2 Gnomon  exon    118137  118262  .   -   .   GeneID:101166461
NC_019859.2 Gnomon  exon    117700  117831  .   -   .   GeneID:101166461
NC_019859.2 Gnomon  exon    117326  117490  .   -   .   GeneID:101166461
我要把下面的东西放出去

NC_019859.2 Gnomon  exon    58334   58504   .   -   .   GeneID:110014915    exon_number:1 
NC_019859.2 Gnomon  exon    54573   54723   .   -   .   GeneID:110014915    exon_number:2
NC_019859.2 Gnomon  exon    52624   52680   .   -   .   GeneID:110014915    exon_number:3
NC_019859.2 Gnomon  exon    52413   52551   .   -   .   GeneID:110014915    exon_number:4
NC_019859.2 Gnomon  exon    28715   28784   .   -   .   GeneID:110014915    exon_number:5
NC_019859.2 Gnomon  exon    26768   26814   .   -   .   GeneID:110014915    exon_number:6
NC_019859.2 Gnomon  exon    25856   25914   .   -   .   GeneID:110014915    exon_number:7
NC_019859.2 Gnomon  exon    25374   25727   .   -   .   GeneID:110014915    exon_number:8
NC_019859.2 Gnomon  exon    70772   70841   .   -   .   GeneID:110017276    exon_number:1
NC_019859.2 Gnomon  exon    70672   70687   .   -   .   GeneID:110017276    exon_number:2
NC_019859.2 Gnomon  exon    70494   70586   .   -   .   GeneID:110017276    exon_number:3
NC_019859.2 Gnomon  exon    69020   69335   .   -   .   GeneID:110017276    exon_number:4
NC_019859.2 Gnomon  exon    68831   68928   .   -   .   GeneID:110017276    exon_number:5
NC_019859.2 Gnomon  exon    68251   68721   .   -   .   GeneID:110017276    exon_number:6
NC_019859.2 Gnomon  exon    89665   89909   .   +   .   GeneID:110014398    exon_number:1
NC_019859.2 Gnomon  exon    91117   91579   .   +   .   GeneID:110014398    exon_number:2
NC_019859.2 Gnomon  exon    119534  120075  .   -   .   GeneID:101166461    exon_number:1
NC_019859.2 Gnomon  exon    118137  118262  .   -   .   GeneID:101166461    exon_number:2
NC_019859.2 Gnomon  exon    117700  117831  .   -   .   GeneID:101166461    exon_number:3
NC_019859.2 Gnomon  exon    117326  117490  .   -   .   GeneID:101166461    exon_number:4
我试过这个命令,结果失败了

awk '{a[$9]++}END{for(i in a){print i, a[i]}}' 
GeneID:110014915 8
GeneID:110017276 6
GeneID:110014398 2
GeneID:101166461 4

提前感谢您,期待您的积极回复。

请尝试以下内容

awk '{print $0,"exon_number:"++a[$9]}'  Input_file
上述代码说明:

print
:是用于打印变量/行的
awk
现成实用程序

$0
:在
awk
语言中,
$0
是当前行(因此打印当前行)

:逗号是分隔符,它将在输出的
$0
和下一个字符串之间输入一个空格

“外显子编号:
:现在根据OP的输出打印字符串
外显子编号

++a[$9]
:我在这里创建一个名为a的数组,其索引为第9列,
++
,然后确保它的值首先增加,然后打印数组a的值(这将是第9列的出现数)


如果您需要将输出作为制表符分开,则在上述代码中将
awk
更改为
awk BEGIN{OFS=“\t”}

你能试试下面的吗

awk '{print $0,"exon_number:"++a[$9]}'  Input_file
awk '$NF!=prev{cnt=0; prev=$NF} {print $0, "exon_number:"++cnt}' file
上述代码说明:

print
:是用于打印变量/行的
awk
现成实用程序

$0
:在
awk
语言中,
$0
是当前行(因此打印当前行)

:逗号是分隔符,它将在输出的
$0
和下一个字符串之间输入一个空格

“外显子编号:
:现在根据OP的输出打印字符串
外显子编号

++a[$9]
:我在这里创建一个名为a的数组,其索引为第9列,
++
,然后确保它的值首先增加,然后打印数组a的值(这将是第9列的出现数)

如果您需要将输出作为制表符分开,则在上述代码中将
awk
更改为
awk BEGIN{OFS=“\t”}

awk '$NF!=prev{cnt=0; prev=$NF} {print $0, "exon_number:"++cnt}' file
这个答案与Ravinders答案的区别在于,his将创建一个由输入文件中的每个键(GeneID)值索引的数组,而上面的方法只使用2个变量,因此使用的内存要少得多。His将用于未排序的文件,而我的将仅在键值按示例输入所示分组在一起时才起作用。从功能上讲,只有当输入文件很大时,内存问题才会起作用


这个答案与Ravinders答案的区别在于,his将创建一个由输入文件中的每个键(GeneID)值索引的数组,而上面的方法只使用2个变量,因此使用的内存要少得多。His将用于未排序的文件,而我的将仅在键值按示例输入所示分组在一起时才起作用。从功能上讲,只有当您的输入文件很大时,内存问题才会起作用。

很好,您向我们展示了您的尝试,尝试将您的示例也包装在代码标签中(请查看您的帖子现在的外观)。编辑帖子时,有一个按钮
{}
,干杯并继续学习,继续在这个伟大的网站上分享。很好,你向我们展示了你的尝试,尝试用代码标签包装你的样本(看看你的文章现在是什么样子)在编辑你的文章时,有一个按钮
{}
,干杯,继续学习,继续在这个伟大的网站上分享。