Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to sort a file that has entries that look like so:

Essie    |   Vaill   |    14225 Hancock Dr       |      Anchorage  |   AK  |   99515 907-345-0962

Please note the "|" stand for tabs of various sizes. I'm trying to sort this by the second field which in this case are last names in alphabetical order. I've tried several different commands such as (note addresses.txt is the name of the file):

sort -k 2 addresses.txt
sort -t$'\t' -k2 addresses.txt
sort -t "`/bin/echo '\t'`" -k 2 addresses.txt

None of these are giving me the desired result and after searching the internet, I simply cannot find a solution that works the way I need it to. If anyone could help me find the way to sort these by the second column in alphabetical order, it would be greatly appreciated.

share|improve this question
have you tried sort -k2,2 addresses.txt? – iruvar Apr 22 '14 at 18:16
Can you give your test and output? – cuonglm Apr 22 '14 at 18:21
this command also did not work. sort -k2,2 addresses.txt – Maximillian Fargo Apr 22 '14 at 18:27
with all of these what ends up happening is that it sorts the last names in an odd way,with the last names not being in the order they are supposed to be in and the first column being sorted in what appears to be the shortest names first and the longest names last. For example, first name Sue is first and first name Zachary is last – Maximillian Fargo Apr 22 '14 at 18:30
up vote 1 down vote accepted

Can you please try:

sort -t"|" -k2 address.txt

I think this shall do it

share|improve this answer
Well the "|" stands for tabs of space, so it doesn't really work, so when I Tried that it ended up sorting by first name or the first column – Maximillian Fargo Apr 22 '14 at 18:25
@MaximillianFargo I tried making a dummy address.txt file, it somehow worked. Actually, I was able to sort using that. Please notice that I am using double quotes. I am not sure if this is relevant, but you are using single quotes in your statements. – Ghassan Apr 22 '14 at 18:27
No I am using double quotes, the issue is that in the actual txt file, the columns aren't separated by "|", they are separated by simply blanks spaces. The actual txt file does not have the "|" anywhere in it – Maximillian Fargo Apr 22 '14 at 18:34
@MaximillianFargo Then you can change the delimiter. -t" " or -t"\t" might help you get it done. I thought that the line you pasted above was extracted from the file of concern. You can set the delimiter to anything that is in the file, and see if it works :) – Ghassan Apr 22 '14 at 18:37
When using "\t" i get this message sort: multi-character tab `\\t' – Maximillian Fargo Apr 22 '14 at 18:39

A more generalized approach in Perl. This might help you in case the blanks are encoded differently from regular spaces or tabs:

perl -aF'[[:blank:]]+' -nle '
        print for sort { $lines{$a}[1] cmp $lines{$b}[1] } keys %lines
    }' your_file

I can't vouch for how it will scale compared to a sort-based solution, though.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.