Here is a gnu awk
version (due to RS
and RT
)
echo "up2left3right" | awk '{for (i=1;i<=(RT?RT:1);i++) if (NF) print $0}' RS="[0-9]"
up
up
left
left
left
right
It changes the Record Separator to a number.
Then the RT
stores the RS
used and we uses a loop to repeat the data.
Updated version that now works with number larger than 9
(multiple digits)
Also added \n
in RS
to work correctly at end of line and if multiple line
awk '{i=RT=="\n"?1:RT;while(i--) if (NF) print $0}' RS="[0-9]+|\n" file
echo -e "up3left12right\ntest4more" | awk '{i=RT=="\n"?1:RT;while(i--) if (NF) print $0}' RS="[0-9]+|\n"
up
up
up
left
left
left
left
left
left
left
left
left
left
left
left
right
test
test
test
test
more