-1

I have a text file that looks like this:

13foobar@example.com
foo.bar3@example2.com
qwerty-1@dept.example3.com

How can I use sed or perl to remove all non-alpha characters before the @ sign. I.e. the desired output would be:

foobar@example.com
foobar@example2.com
qwerty@dept.example3.com
user254173
  • 152
  • 6

2 Answers2

1

The following one-liner should suffice.

perl -pe 's/[^a-z]+(?=[^@\n]*@)//g'
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • Generally speaking, this a bad approach since it's O(N^2). However, since the OP's input will be limited in length in practice, that's not relevant for him. – ikegami Aug 26 '15 at 03:35
  • @ikegami, In general I certainly agree, but for this case it would be alright. – hwnd Aug 26 '15 at 03:42
0

Input

13foobar@example.com
foo.bar3@example2.com
qwerty-1@dept.example3.com

Perl one-liner

perl -ne 'my ($user,$host) = split /\@/; $user =~ s/[\W\d]//g; print $user . "@" . $host;'

In action

cat input | perl -ne 'my ($user,$host) = split /\@/; $user =~ s/[\W\d]//g; print $user . "@" . $host;'

Output

foobar@example.com
foobar@example2.com
qwerty@dept.example3.com
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38