108

I have a file like this:

FirstName, FamilyName, Address, PhoneNumber

How can I sort it by FamilyName?

Rami Jarrar
  • 4,523
  • 7
  • 36
  • 52

4 Answers4

184

If this is UNIX:

sort -k 2 file.txt

You can use multiple -k flags to sort on more than one column. For example, to sort by family name then first name as a tie breaker:

sort -k 2,2 -k 1,1 file.txt

Relevant options from "man sort":

-k, --key=POS1[,POS2]

start a key at POS1, end it at POS2 (origin 1)

POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

-t, --field-separator=SEP

use SEP instead of non-blank to blank transition

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Just be a little careful to use `--field-separator=','` if you might have a data entry operator type in values for "First name" like "Billy Bob" or whatever... spaces can easily get into your data if you don't guard against it, but commas are relatively unlikely. – Tony Delroy Nov 24 '10 at 05:04
  • 1
    There are very likely cases of commas in those fields, like "Smith, Jr." or "McDowell, Sr." or "Dr. John" or "New York, NY" – jbnunn Feb 05 '14 at 23:58
  • 3
    Note that if the columns are visually aligned, i.e. there is a non constant number of spaces between each fields, you must use the `-b` option. This is because `sort` is actually considering that the string to sort starts just after the comma, and not from the first letter of the column. Also, you may need to prefix the command with `LC_ALL=C`, to avoid any side effect due to the locale, which can happen even on a simple ASCII file. – calandoa Apr 15 '15 at 18:01
  • @calandoa Thanks for the part on `-b` (`--ignore-leading-blanks`). To clarify a bit: `echo -e 'aaa\nab' | sort -k2` gives `ab` first (the second column starts after the first `non-blank to blank transition`, and `b` is before `a`), but with `-b` it gives `aaa` as expected (`a` is before `b`). – Kirill Bulygin Nov 11 '17 at 11:07
10
sort -nk2 file.txt

Accordingly you can change column number.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Dheeraj Kumar
  • 101
  • 1
  • 2
7

To sort by second field only (thus where second fields match, those lines with matches remain in the order they are in the original without sorting on other fields) :

sort -k 2,2 -s orig_file > sorted_file
Stephan
  • 41,764
  • 65
  • 238
  • 329
Cian
  • 88
  • 1
  • 5
3

FWIW, here is a sort method for showing which processes are using the most virt memory.

memstat | sort -k 1 -t':' -g -r | less

Sort options are set to first column, using : as column seperator, numeric sort and sort in reverse.

netskink
  • 4,033
  • 2
  • 34
  • 46