3

I would like to sort an email address list in a file by domain in bash.

$ cat file.txt
abc@abc.net
bbb@aaa.org
aba@aaa.com
aaa@aaa.com
ccc@abb.com
aba@abb.com
abc@abc.com

I tried with sort but it sorts only beginning with the username.

$ sort file.txt
aaa@aaa.com
aba@aaa.com
aba@abb.com
abc@abc.com
abc@abc.net
bbb@aaa.org
ccc@abb.com

I would like to sort first domain then username.

6ickm8nit
  • 33
  • 3

1 Answers1

5
$ sort -t @ -k2 file
aaa@aaa.com
aba@aaa.com
bbb@aaa.org
aba@abb.com
ccc@abb.com
abc@abc.com
abc@abc.net

man sort:

-t, --field-separator=SEP
       use SEP instead of non-blank to blank transition

-k, --key=KEYDEF
       sort via a key; KEYDEF gives location and type
James Brown
  • 36,089
  • 7
  • 43
  • 59