对行组进行排序

发布于 2024-08-22 15:10:29 字数 1523 浏览 4 评论 0原文

我有一个文本文件,如下所示,

iv_destination_code_10
TAP310_mapping_RATERUSG_iv_destination_code_10
RATERUSG.iv_destination_code_10 = WORK.maf_feature_info[53,6]
iv_destination_code_2
TAP310_mapping_RATERUSG_iv_destination_code_2
RATERUSG.iv_destination_code_2 = WORK.maf_feature_info[1,6]
iv_destination_code_3
TAP310_mapping_RATERUSG_iv_destination_code_3
RATERUSG.iv_destination_code_3 = WORK.maf_feature_info[7,6]
iv_destination_code_4
TAP310_mapping_RATERUSG_iv_destination_code_4
RATERUSG.iv_destination_code_4 = WORK.maf_feature_info[13,6]
iv_destination_code_5
TAP310_mapping_RATERUSG_iv_destination_code_5
RATERUSG.iv_destination_code_5 = WORK.maf_feature_info[19,6]
iv_destination_code_6
TAP310_mapping_RATERUSG_iv_destination_code_6
RATERUSG.iv_destination_code_6 = WORK.maf_feature_info[29,6]
iv_destination_code_7
TAP310_mapping_RATERUSG_iv_destination_code_7
RATERUSG.iv_destination_code_7 = WORK.maf_feature_info[35,6]
iv_destination_code_8
TAP310_mapping_RATERUSG_iv_destination_code_8
RATERUSG.iv_destination_code_8 = WORK.maf_feature_info[41,6]
iv_destination_code_9
TAP310_mapping_RATERUSG_iv_destination_code_9
RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

三行组合形成一个单元:

    iv_destination_code_9
    TAP310_mapping_RATERUSG_iv_destination_code_9
    RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

是一个单元。

iv_destination_code_9

9 表示我必须排序的数字 10 9 8....

我需要一个 shell 脚本/awk,它将按降序对单位进行排序。 这怎么可能?

I have a text file like below

iv_destination_code_10
TAP310_mapping_RATERUSG_iv_destination_code_10
RATERUSG.iv_destination_code_10 = WORK.maf_feature_info[53,6]
iv_destination_code_2
TAP310_mapping_RATERUSG_iv_destination_code_2
RATERUSG.iv_destination_code_2 = WORK.maf_feature_info[1,6]
iv_destination_code_3
TAP310_mapping_RATERUSG_iv_destination_code_3
RATERUSG.iv_destination_code_3 = WORK.maf_feature_info[7,6]
iv_destination_code_4
TAP310_mapping_RATERUSG_iv_destination_code_4
RATERUSG.iv_destination_code_4 = WORK.maf_feature_info[13,6]
iv_destination_code_5
TAP310_mapping_RATERUSG_iv_destination_code_5
RATERUSG.iv_destination_code_5 = WORK.maf_feature_info[19,6]
iv_destination_code_6
TAP310_mapping_RATERUSG_iv_destination_code_6
RATERUSG.iv_destination_code_6 = WORK.maf_feature_info[29,6]
iv_destination_code_7
TAP310_mapping_RATERUSG_iv_destination_code_7
RATERUSG.iv_destination_code_7 = WORK.maf_feature_info[35,6]
iv_destination_code_8
TAP310_mapping_RATERUSG_iv_destination_code_8
RATERUSG.iv_destination_code_8 = WORK.maf_feature_info[41,6]
iv_destination_code_9
TAP310_mapping_RATERUSG_iv_destination_code_9
RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

combination of three lines form a unit:

    iv_destination_code_9
    TAP310_mapping_RATERUSG_iv_destination_code_9
    RATERUSG.iv_destination_code_9 = WORK.maf_feature_info[47,6]

is one unit.

iv_destination_code_9

9 indicates the number by which i have to sort
10
9
8....

i need a shell script/awk which will sort the units in a descending order.
how is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎无痕… 2024-08-29 15:10:29
cat file | tr '\n' '#' | sed 's/]#/]\n/g' | sort -nrt_ -k4 | tr '#' '\n'

首先,所有行尾都替换为 #,并重新创建块末尾的行尾 (]#)。

然后对第四个字段执行数字反向排序,字段之间用 _ 分隔。

最后,检索原始行尾。

cat file | tr '\n' '#' | sed 's/]#/]\n/g' | sort -nrt_ -k4 | tr '#' '\n'

First all end of lines are replaced by #, and end of lines at the end of blocks (]#) are recreated.

Then a numeric reverse sort is performed on the fourth field with fields separated by _.

Finally, original end of lines are retrieved.

还给你自由 2024-08-29 15:10:29
sed 'N;N;s/\n/#/g' file |sort -t"_" -nr -k4 | sed 's|#|\n|g'

或与呆呆

awk -vRS="\niv_" -vFS="\n" 'BEGIN{t=0}
{
 m=split($1,a,"_")
 num[a[m]]
 line[a[m]] = $0
}
END{
 cmd="sort -nr"
 for(i in num){ print i |& cmd }
    close(cmd,"to")
    while((cmd |& getline m) > 0) {
        z=split(m,arr2,"\n")
    }
    close(cmd,"from")
 print line[ arr2[1] ]
 for(j=2;j<=z;j++){
    if(line[ arr2[j]] != "" ){
        print "iv_"line[ arr2[j] ]
    }
 }
}' file
sed 'N;N;s/\n/#/g' file |sort -t"_" -nr -k4 | sed 's|#|\n|g'

Or with gawk

awk -vRS="\niv_" -vFS="\n" 'BEGIN{t=0}
{
 m=split($1,a,"_")
 num[a[m]]
 line[a[m]] = $0
}
END{
 cmd="sort -nr"
 for(i in num){ print i |& cmd }
    close(cmd,"to")
    while((cmd |& getline m) > 0) {
        z=split(m,arr2,"\n")
    }
    close(cmd,"from")
 print line[ arr2[1] ]
 for(j=2;j<=z;j++){
    if(line[ arr2[j]] != "" ){
        print "iv_"line[ arr2[j] ]
    }
 }
}' file
从﹋此江山别 2024-08-29 15:10:29

这与 mouvicel 的答案类似,但使用非打印字符作为特殊标记(并假设原始文件不包含它们)。

sed 's/]$/]'

它假设原始文件中没有空行,因为它在末尾删除了它们。它还依赖于每个组结束行以“]”结尾。

\1''/' text_file | tr '\1' '\0' | sort -znrt_ | tr '\0' '\n' | sed '/^$/d'

它假设原始文件中没有空行,因为它在末尾删除了它们。它还依赖于每个组结束行以“]”结尾。

This works similarly to mouvicel's answer, but uses non-printing characters as the special markers (and assumes that the original file doesn't contain them).

sed 's/]$/]'

It assumes that there are no blank lines in the original file since it deletes them at the end. It also relies on every group-ending line to end in "]".

\1''/' text_file | tr '\1' '\0' | sort -znrt_ | tr '\0' '\n' | sed '/^$/d'

It assumes that there are no blank lines in the original file since it deletes them at the end. It also relies on every group-ending line to end in "]".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文