Linux 如何为排序后的输出提供秩号?
例如,如果输入为:Linux 如何为排序后的输出提供秩号?,linux,shell,unix,Linux,Shell,Unix,例如,如果输入为: Dog 100 Cat 200 Monkey 50 Elephant 20 我希望输出为: 1 Cat 200 2 Dog 100 3 Monkey 50 4 Elephant 20 我想使用sort函数来实现这一点。谢谢 以便对每行的第二个元素(-k2)进行排序,然后添加行nbr(|nl)。-r选项用于颠倒顺序 sort -k2 -rn file.txt | nl 其中: 1 Cat 200 2 Dog 100 3 Monkey 50 4 Elepha
Dog 100
Cat 200
Monkey 50
Elephant 20
我希望输出为:
1 Cat 200
2 Dog 100
3 Monkey 50
4 Elephant 20
我想使用sort函数来实现这一点。谢谢 以便对每行的第二个元素(
-k2
)进行排序,然后添加行nbr(|nl
)。-r
选项用于颠倒顺序
sort -k2 -rn file.txt | nl
其中:
1 Cat 200
2 Dog 100
3 Monkey 50
4 Elephant 20
请出示你的密码。由于堆栈溢出对您隐藏了关闭原因:寻求调试帮助的问题(“为什么此代码不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。