I have a string like AxxBCyyyDEFzzLMN and I want to replace all the occurrences of x, y, and z with _.
How can I achieve this?
I know that echo "$string" | tr 'x' '_' | tr 'y' '_'
would work, but I want to do that in one go, without using pipes.
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.