You can capture parts of the matched pattern with \(..\)
and reuse it in your replacement string using \1
:
echo mike@yahoo.com | sed 's/[a-zA-Z0-9][a-zA-Z0-9]*@\([a-zA-Z0-9][a-zA-Z0-9]*\.[a-zA-Z0-9][a-zA-Z0-9]*\)/customer@\1/g'
سؤال
Assume the regular expression of a email is [a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z0-9]+, I would like to substitute all the id part into "customer", for example
mike@yahoo.com => customer@yahoo.com john@gmail.com => customer@gmail.com
I can write something like
$ echo mike@yahoo.com | sed 's/[a-zA-Z0-9][a-zA-Z0-9]*@[a-zA-Z0-9][a-zA-Z0-9]*\.[a-zA-Z0-9][a-zA-Z0-9]*/customer/g'
But how can I get the domain part not changed? Basically, the question is find a pattern in a string and substitute just part of it and the remaining part not changed.
المحلول
You can capture parts of the matched pattern with \(..\)
and reuse it in your replacement string using \1
:
echo mike@yahoo.com | sed 's/[a-zA-Z0-9][a-zA-Z0-9]*@\([a-zA-Z0-9][a-zA-Z0-9]*\.[a-zA-Z0-9][a-zA-Z0-9]*\)/customer@\1/g'
نصائح أخرى
I think you're making it too complicated:
echo mike@yahoo.com | sed -e 's/.*@/customer@/'