So these are the original texts: $ printf 'bbb\nb2b\n'bbbb2bThis is what I'm doing$ printf 'bbb\nb2b\n' | sed 's/^b[0-9]/XXX/g'bbbXXXbAnd this is the output that I want :D$ printf 'bbb\nb2b\n' | SOME_SED_MAGICbbbXXX2bMy question: How can I swap the b2b to XXX2b, so I put back the 2 from the sed match? 2 could vary. 
How to sed and put back the part of the match?
sed
You can use backreference:$ printf 'bbb\nb2b\n' | sed 's/^b\([0-9]\)/XXX\1/'bbbXXX2b(BTW, you don't need the g  flag since that regex can match only once because of the ^).