sed/awk, instead of Perl

Oliver Fromme olli at
Thu Aug 21 13:49:19 UTC 2008

Steve Bertrand wrote:
 > To put it plainly, can anyone, if it's possible, provide a single line 
 > sed/awk pipeline that can:
 > - read email addresses from a file in the format:
 > TAB domain.tld
 > - convert it to:
 > user_name at

With awk(1):

    awk '{sub(/\./, "_", $1); print $1 ""}'

With sed(1) (you have to replace "^T" with a real tab!):

    sed 's/^\(.*\)\.\(.*\)^T.*/\1_\2 at'

With tr(1) + sed(1):

    tr '.\t' '_@' | sed 's/@.*/'

Personally I like the last solution best, because it's
short and easy to understand.

BTW, all of the above command read from stdin and write
to stdout, so you can use shell redirection to read from
a file and/or write to a file.  There is no need to use

Best regards

Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:

With Perl you can manipulate text, interact with programs, talk over
networks, drive Web pages, perform arbitrary precision arithmetic,
and write programs that look like Snoopy swearing.

More information about the freebsd-questions mailing list