Search code examples
awkmultiple-columns

Collapsing a column value into lines, copying values of a second column


I have a file with two columns (tab-separated): In the first column I have the number of lines that I want to collapse, and in the second column is the number that I want to be pasted in each row (in a new file), based on the first column values.

File1:

col1 col2
365 1
6   1
142 1
99  0
223 0
11  1

So basically in the new file I want 365 lines with the number 1, followed by 6 lines of 1, 142 lines of 1, 99 lines of 0, 223 lines of 0 and 11 lines of 1...and so forth... In total the new file should have 846 lines (which is the sum of the first column on the File1.

Ideally an awk command should do the trick I guess. Any inputs on this would be really appreciated... Thanks


Solution

  • I would use GNU AWK following way. Contrived example to avoid superlong output, let file.txt be

    col1 col2
    5 1
    3 0
    5 1
    

    then

    awk 'NR>1{for(i=0;i<$1;i+=1)print $2}' file.txt
    

    output

    1
    1
    1
    1
    1
    0
    0
    0
    1
    1
    1
    1
    1
    

    Explanation: I used for statement to print content of 2nd column ($2) times specified in 1st column ($1) for every line beyond 1st line (NR>1).

    (tested in gawk 4.2.1)