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 postthis SO post for examples involving sed, perl and awk.