Donngal
December 10, 2012, 5:08am
1
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.
Donngal
December 10, 2012, 5:23am
3
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
Donngal
December 10, 2012, 5:29am
5
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.
donngal:
thank you, that was the problem. But * is another thing than +, isn't it? * does accept zero characters after the @, too or not?
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