print the number in second col the number of times in a new line always as specified in col 1

1

how do I print the value in col 2 as many times as it is specified in col 1.

10 -11
9 -10
7 -9
7 -8
7 -7
12 -6
362 -1
271 1
94 2
41 3
17 4
13 5
16 6
5 7

expected output:

-11
-11
-11
-11
-11
-11
-11
-11
-11
-11.....so on and so forth...

Gavin

Posted 2019-04-18T21:48:56.947

Reputation: 13

Can you explain it some more and give a better example? If the expected output is -11 -11 -11 and so on, continuing that pattern seems rather easy. – Alex Cannon – 2019-04-18T21:51:49.320

There are two columns each have values. How can we print the value in the second col as many times as it is stated in the first col? If the value is -11 in col. 2 and the corresponding value in col 1 is 10 then it should be repeated 10 times. The value -11 should be printed in a new line each time. Similarly, this should be repeated for all the values in col 2 corresponding to the value in col 1. – Gavin – 2019-04-18T21:54:25.493

I understand now. If column 1 row 1 contains 4, and column 2 row 1 contains 6, it should print out 6 6 6 6 with each 6 on a new line. – Alex Cannon – 2019-04-18T21:57:40.280

Answers

1

This perl one-liner does the job:

perl -ane '($x,$y)=split;print"$y\n"x$x' file

Outoout:

-11
-11
-11
-11
-11
-11
-11
-11
-11
-11
-10
-10
-10
-10
-10
-10
-10
-10
-10
...

Explanation:

($x,$y)=split;      # split the values from each line ($x=first value, $y=second value)
print"$y\n"x$x      # print $x times the value $y followed by line break

Toto

Posted 2019-04-18T21:48:56.947

Reputation: 7 722