Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: sort email address list by domain

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

$ cat file.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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

$ sort file.txt
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

I would like to sort first domain then username.

like image 216
6ickm8nit Avatar asked Sep 14 '25 01:09

6ickm8nit


1 Answers

$ sort -t @ -k2 file
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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
like image 59
James Brown Avatar answered Sep 16 '25 16:09

James Brown