views:

48

answers:

2

folks, i have a string like

AxxBCyyyDEFzzLMN

I want to replace all x and y and z with _ so that the output is

 A_BC_DEF_LMN

How to do that?

I know a series of

echo "$string" | tr 'x' '_' | tr 'y' '_' 

will work, but I want to do taht in one go, without using pipes

EDIT: The following worked

echo "$string" | tr '[xyz]' '_'

thanks in advance

+2  A: 
orig="AxxBCyyyDEFzzLMN"
mod=${orig//[xyz]/_}
Matthew Flaschen
thanks. i wanted in this form though ... (see my edit avove)
Amarsh
+2  A: 
echo "$string" | tr xyz _

would replace each occurrence of x, y, or z with , giving A_BC__DEF_LMN in your example.

echo "$string" | sed -r 's/[xyz]+/_/g'

would replace repeating occurrences of x, y, or z with a single _, giving A_BC_DEF_LMN in your example.

jkasnicki