Pregunta

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.

¿Fue útil?

Solución

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'

Otros consejos

I think you're making it too complicated:

echo mike@yahoo.com | sed -e 's/.*@/customer@/'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top