質問

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@/'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top