Search code examples
linuxbashawksedcsh

Insert line from file 2 into file 1 after certain letter/symbol


I'm trying to combine two files at certain locations using awk/sed/forloop/foreach... whichever is the most simple.

First, I have file 1 which has ...

#   
#   
>  
#    
#     
>

etc..

And in my second file, I have just numbers (as many numbers as >'s in the other file).

Num1  
Num2   
Num3

etc...

I want to insert the numbers in order in file 2, after each > in the first file, such as...

#   
#   
> Num1  
#    
#     
> Num2

Thanks!


Solution

  • Short awk solution:

    awk '/^>/{ r=$0; if ((getline < "file2") > 0) $0=r OFS $0 }1' file1
    

    Sample output:

    #
    #
    > Num1
    #
    #
    > Num2