sed pattern &

Hi,

I can't seem to understand what the sed and & do.. Why is the set of numbers appear twice ? how the command really work ?

echo "123 abc" | sed 's/[0-9]*/& www &/'


Output: 
123 www 123 abc

man sed yields:

       s/regexp/replacement/
              Attempt to match regexp against the pattern space.  If  success
              ful,   replace  that  portion  matched  with  replacement.   The
              replacement may contain the special character & to refer to that
              portion  of  the  pattern  space  which matched, and the special
              escapes \1 through \9 to refer  to  the  corresponding  matching
              sub-expressions in the regexp.

I understood the supposed function of sed with &, but this explanation (I read something similar before posting) doesn't explain to me why the set of numbers appears twice, and how the output came to be this way..

the numbers appear twice because there're 2 &-s separated by www.

echo "123 abc" | sed 's/[0-9]*/& www &/'
1 Like