Another Perl version: perl -pe 's/(<.)(.*)(@.)(.*)(.\..*>)/$1."*" x length($2).$3."*" x length($4).$5/e' Example: $ perl -pe 's/(<.)(.*)(@.)(.*)(.\..*>)/$1."*" x length($2).$3."*" x length($4).$5/e' foo John Doe <j*******@g***l.com> John Doe <j******@h*****l.net> Jane Doe <j****@o*****k.org> With `sed`, replacing the characters by an equal number of `*` is complicated. See [this SO post][1] for examples involving `sed`, `perl` and `awk`. [1]: https://stackoverflow.com/q/16005578/2072269