根据文件的第二列对数据进行排序 [英] Sorting data based on second column of a file

查看:154
本文介绍了根据文件的第二列对数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个两列且行数为n的文件.

I have a file of two columns and n number of rows.

第1列包含names和第2列age.

我想基于age(在第二栏中)以升序对文件的内容进行排序.

I want to sort the content of this file in ascending order based on the age (in second column).

结果应显示最小的人的namename,然后显示第二个最小的人,依此类推...

The result should display the name of the youngest person along with name and then second youngest person and so on...

关于一个线性shell或bash脚本的任何建议.

Any suggestions for a one liner shell or bash script.

推荐答案

您可以使用 sort命令:

You can use the sort command:

sort -k2 -n yourfile

-n--numeric-sort根据字符串数值进行比较

-n, --numeric-sort compare according to string numerical value

例如:

$ cat ages.txt 
Bob 12
Jane 48
Mark 3
Tashi 54

$ sort -k2 -n ages.txt 
Mark 3
Bob 12
Jane 48
Tashi 54

这篇关于根据文件的第二列对数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆