Search code examples
bashawkblank-line

Insert multiple blanks every n rows


I am trying to insert multiple blank lines after every n row in a data file.

So far I have this line which inserts 1 blank line after every 1 row:

awk '1 ; !(NR % 1) {print "";}' in.file >> out.file

And then I try to loop it so I insert 10 lines every 1 row:

awk '1; {for(i=0; i<10; i++)} !(NR % 1) {print "";}'

But I don't think I have the syntax correct. Any suggestions?

Ideal output would be to take take input-A and paste it to input-B so that it looks like this:

(1) input-A

>1   
>0   
>1 

(2) input-B

>1000    
>... ... ...  
>... ... ...  
>... ... ...  
>2000    
>... ... ...  
>... ... ...  
>... ... ...  
>3000    
>... ... ...  
>... ... ...  
>... ... ...  

(3) pasted input-A and input-B

>1  1000    
>   ... ... ...  
>   ... ... ...  
>   ... ... ...  
>0  2000    
>   ... ... ...  
>   ... ... ...  
>   ... ... ...  
>1  3000    
>   ... ... ...  
>   ... ... ...  
>   ... ... ... 

Hence why I'm trying to create the right amount of spaces between the lines of input-A so that it easily pastes with input-B.


Solution

  • To insert 2 blank lines every 3 input lines:

    $ seq 10 |
        awk -v n=2 -v m=3 '
            BEGIN {
                lines = sprintf("%*s",n-1,"")
                gsub(/ /,ORS,lines)
            }
            { print }
            (NR%m) == 0 {
                print lines
            }
        '
    1
    2
    3
    
    
    4
    5
    6
    
    
    7
    8
    9
    
    
    10
    

    To print 12,000 blank lines every 10 input lines instead, just change n=2 to n=12000 and m=3 to m=10.

    If that's not what you want then edit your question to clarify your requirements and provide concise, testable sample input and expected output.

    wrt your updated question, depending on what those ...s in inputB are, this might be what you really should be using:

    $ awk '
        NR==FNR { a[NR]=$0; next }
        {
            sub(/^>/,"")
            print (NF==1 ? a[++c] : ">") OFS $0
        }
    ' inputA inputB
    >1 1000
    > ... ... ...
    > ... ... ...
    > ... ... ...
    >0 2000
    > ... ... ...
    > ... ... ...
    > ... ... ...
    >1 3000
    > ... ... ...
    > ... ... ...
    > ... ... ...