Remove single @ on line from file

Hi All,

So I have to remove all the @hostnames from a file, the problem is, there are instances where @ is used for other things... For example:

example text:

@This is some text in between some at signs@
@This is some more text@
This is a line that will contain a username and his/her email - me@somehost.com
This is another email line - you@anotherhost.com

I need to remove the text after the @somehost.com and @anotherhost.com but leave the two previous @ bla bla bla @ lines.

Heres what I have tried:

cat * |egrep -v '@.*@'|sed 's/@.*/@REDACTED/'

This kind of works, but I need all the lines intact

sed 's/@.*/@REDACTED/'

This just removed all the test after the @. This would be ok, but we would like to have the other lines if possible - we do just want to secure the file from having hostnames.

Not sure how to accomplish this. Any advice would be appreciated.

Thanks!
Joe

Hi

sed 's/@\w\+\.\w\+/@REDACTED/g'

or

sed 's/@\w\+\.\w\+/@REDACTED/3'
1 Like

for a non-GNU sed - might be harden a bit more/better:

sed 's/@[^ ][^ ]*[.][^ ][^ ]*/@REDACTED/g' myFile
1 Like

Thanks for your help! I used

sed 's/@\w\+\.\w\+/@REDACTED/g'

Works with any Posix-compatible sed:

sed 's/\([[:alnum:]]\)@[[:alnum:]][-_.[:alnum:]]*/\1@REDACTED/g' file

By requiring a character before the @ it will not trigger on a @ at the beginning of the line.

1 Like

And a lookbehind perlie you could try

perl -pe 's/(?<=\S@)\S+/\REDACTED/g' file