可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a file of two columns and n
number of rows.
column 1 contains names
and column2 age
.
I want to sort the content of this file in ascending order based on the age
(in second column).
The result should display the name
of the youngest person along with name
and then second youngest person and so on...
Any suggestions for a one liner shell or bash script.
回答1:
You can use the sort
command:
sort -k2 -n yourfile
For example:
$ cat ages.txt Bob 12 Jane 48 Mark 3 Tashi 54 $ sort -k2 -n ages.txt Mark 3 Bob 12 Jane 48 Tashi 54
回答2:
Solution:
sort -k 2 -n filename
more verbosely written as:
sort --key 2 --numeric-sort filename
Example:
$ cat filename A 12 B 48 C 3 $ sort --key 2 --numeric-sort filename C 3 A 12 B 48
Explanation:
-k # - this argument specifies the first column that will be used to sort. (note that column here is defined as a whitespace delimited field; the argument -k5
will sort starting with the fifth field in each line, not the fifth character in each line)
-n - this option specifies a "numeric sort" meaning that column should be interpreted as a row of numbers, instead of text.
More:
Other common options include:
- -r - this option reverses the sorting order. It can also be written as --reverse.
- -i - This option ignores non-printable characters. It can also be written as --ignore-nonprinting.
- -b - This option ignores leading blank spaces, which is handy as white spaces are used to determine the number of rows. It can also be written as --ignore-leading-blanks.
- -f - This option ignores letter case. "A"=="a". It can also be written as --ignore-case.
- -t [new separator] - This option makes the preprocessing use a operator other than space. It can also be written as --field-separator.
There are other options, but these are the most common and helpful ones, that I use often.
回答3:
Use sort
.
sort ... -k 2,2 ...
回答4:
For tab separated values the code below can be used
sort -t$'\t' -k2 -n
-r can be used for getting data in descending order.
-n for numerical sort
For descending order below is the code
sort -t$'\t' -k2 -rn