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.

  • have you tried sort -k2,2 addresses.txt?
    – iruvar
    Apr 22, 2014 at 18:16
  • Can you give your test and output?
    – cuonglm
    Apr 22, 2014 at 18:21
  • this command also did not work. sort -k2,2 addresses.txt Apr 22, 2014 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 Apr 22, 2014 at 18:30

2 Answers 2


Can you please try:

sort -t"|" -k2 address.txt

I think this shall do it

  • 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 Apr 22, 2014 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, 2014 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 Apr 22, 2014 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, 2014 at 18:37
  • When using "\t" i get this message sort: multi-character tab `\\t' Apr 22, 2014 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .