6ickm8nit
6ickm8nit

Reputation: 43

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.

Upvotes: 4

Views: 2144

Answers (1)

James Brown
James Brown

Reputation: 37464

$ 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

Upvotes: 6

Related Questions