You can also use a capture group. A capture is used to grab a part of the match and save it into an auxiliary variable, that is named numerically in the order that the capture appears.
echo apple1 | sed -e 's/\(a\)\(p*\)\(le\)1/\1\2\32/g'
We used three captures:
- The first one, stored in
\1
, contains an "a"
- The second one, stored in
\2
, contains a sequence of "p"s (in the example it contains "pp")
- The third one, stored in
\3
, contains the sequence "le"
Now we can print the replacement using the matches we captured: \1\2\32
. Notice that we are using 3 capture values to generate "apple" and then we append a 2. This wont be interpreted as variable \32
because we can only have a total of 9 captures.
Hope this helps =)