joe joe - 27 days ago 8
Bash Question

shell how to repeat line in a text with the value of the end of the line

I need the following text to be repeated with the number at the end of the line with bash:

test1, red, 1
test2, green, 2
test3, white, 3


this need to give the following result

test1, red
test2, green
test2, green
test3, white
test3, white
test3, white


Please explain the steps if needed.

Answer

Wtih awk you could do something like this:

$ awk 'BEGIN{FS=OFS=", "}{for(i=1;i<=$NF;i++)print $1,$2}' file
test1, red
test2, green
test2, green
test3, white
test3, white
test3, white
Comments