Reputation: 13
I would like to repeat some values correspond to a number of previous column.
I have this input:
2 miR-5
3 miR-10
1 miR-4
4 miR-6
I would like this output:
miR-5
miR-5
miR-10
miR-10
miR-10
miR-4
miR-6
miR-6
miR-6
miR-6
Upvotes: 1
Views: 66
Reputation: 33327
I think that bash is wrong language for this task, it's not optimized for text manipulation, it's best suited for tasks where you want to spawn some processes or manipulate files etc. In most cases simple text manipulation tasks can be done with awk. The following produces the desired output for your input file:
$ cat file
2 miR-5
3 miR-10
1 miR-4
4 miR-6
$ awk '{for (i=1; i<=$1; i++) print $2}' file
miR-5
miR-5
miR-10
miR-10
miR-10
miR-4
miR-6
miR-6
miR-6
miR-6
For completeness with the tags in the question, here is how you would do it in bash:
#!/bin/bash
while read -r num rest
do
for ((i=1;i<=num;i++))
do
printf '%s\n' "$rest"
done
done < file
Upvotes: 4