Basic sed question

Please have a look at below examples. Why do these 3 sed commands deliver the same result? Especially, why are there 4 "x" in the result instead of 3?

echo "abc" | sed 's/d*/x/g'
xaxbxcx

echo "abc" | sed 's/d*/&x/g'
xaxbxcx

echo "abc" | sed 's/d*/x&/g'
xaxbxcx

Thanks for any help on this.

d* means zero or more d's in a row. In the string "abc" There are zero d's before the "a", after the "c" and in between each letter. The & is the string of d's that were matched. This allows you to place the zero d's back in varying places resulting in no visible effect. Put some d's in the initial string and try that. Then you will see different results.

1 Like

Thanks a lot. This is clear to me now.