sed noob needs help with replacement pattern.

Hi there,

i am absolutely new in shell programming and especially using sed. What i want to do is to replace every emailaddress suffix with another. In my Testfile there is:

foo@bar.com

My attempt to replace every @<something>.<someotherthing> is:

sed 's/@[a-z0-9A-Z]+\.[a-zA-Z0-9]+/REPLACE/g' test.txt >test_replaced.txt

But this isnt working. Is my RegEx wrong or do i use sed wrong?

$ echo "test@abc.com" | sed 's/@[A-Za-z0-9]*\.[A-Za-z0-9]*/@REPLACE.REPLACE/'                                     
test@REPLACE.REPLACE
$ echo "123@213sdcsdc.com" | sed 's/@[A-Za-z0-9]*\.[A-Za-z0-9]*/@REPLACE.REPLACE/' 
123@REPLACE.REPLACE

---------- Post updated at 03:46 PM ---------- Previous update was at 03:45 PM ----------

In some sed version, + will not work as expected.

thank you, that was the problem. But * is another thing than +, isn't it? * does accept zero characters after the @, too or not?

yes

 
$ echo "123@.com" | sed 's/@[A-Za-z0-9]*\.[A-Za-z0-9]*/@REPLACE.REPLACE/'
123@REPLACE.REPLACE
$ echo "123@." | sed 's/@[A-Za-z0-9]*\.[A-Za-z0-9]*/@REPLACE.REPLACE/'
123@REPLACE.REPLACE

ok, thx, i have to find a solution for that, then. But now i know the problem, and the rest ist another problem, i will find the solution by myself.

Thank you so far.

Edit:

Calling sed as

sed -r

to accept extended regular expressions does the trick for me.

If you want to reproduce sed's + character to * then try as below

sed 's/@[a-z0-9A-Z][a-z0-9A-Z]*\.[a-zA-Z0-9][a-zA-Z0-9]*/REPLACE/g'
1 Like