Sorting data based on second column of a file

BashShellUnix

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

You can use the key option of the sort command, which takes a "field number", so if you wanted the second column:

sort -k2 -n yourfile

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

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

Solution 2 - Bash

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.

Solution 3 - Bash

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
-k, --key=POS1[,POS2] where k is column in file
For descending order below is the code

sort -t$'\t' -k2 -rn

Solution 4 - Bash

Use sort.

sort ... -k 2,2 ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAngeloView Question on Stackoverflow
Solution 1 - BashMatt RyallView Answer on Stackoverflow
Solution 2 - BashDCurroView Answer on Stackoverflow
Solution 3 - BashSaurabhView Answer on Stackoverflow
Solution 4 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow