Printf with backreference in ruby

Hello everybody,

May you help with this, I don't know what is wrong.

I want trying to print the first 4 characters as decimal and remove the "k's" from the next 7 characters.

I'm trying with gsub and backrefence as shown below trying to remove the "k's" and then trying to assign to "x" the 0x0020
using the backreference of pattern1, but is not working.

What I've tried:

string = "0020aakbbjk30294748"

string=string.gsub!(/(\d{4})([a-z]{7})((\d){5})/,'k')

x="0x"+"#{$1}"

printf("%d ",$x) 
puts "#{$2} #{$3}"

desired output:

32 aabbj 30294

Thanks in advance for any help.