Ubuntu – How to remove domain part of each line from text file containing email addresses

command lineemailregextext processing

I have a text file with one email address per line. How can I remove the domain part but not the TLD part of each line? In here, we would define TLD to be the last element of the string after the last dot character.

In other words, sample input:

foobar@example.org
john.smith@hotmail.com
chunkylover69@mail.dk
qwerty@yahoo.co.uk

Desired output:

foobar@org
john.smith@com
chunkylover69@dk
qwerty@uk

Best Answer

$ sed 's/@.*[.]/@/' file
foobar@org
john.smith@com
chunkylover69@dk
qwerty@uk

The regex @.*[.] captures everything from @ to a the last period . on the line. We replace that with @.