0

I have two files A and B.

File A
>Node1                  
...
>Node2
...

File B
>gb|KY551314.1| Influenza A virus (A/mallard/Idaho/AH0011522/2015(H7N7)) segment 
2 polymerase PB1 (PB1) and PB1-F2 protein (PB1-F2) genes, 
complete cds
Length=2316

>gb|KY561069.1| Influenza A virus (A/American green-winged teal/Missouri/15OS6591/2015(H11N9)) 
segment 1 polymerase PB2 (PB2) gene, complete 
cds
Length=2341

How can I replace each NodeX line of File A with the subsequent entries in File B? The result would look like the following:

File A
>gb|KY551314.1| Influenza A virus (A/mallard/Idaho/AH0011522/2015(H7N7)) segment 2 polymerase PB1 (PB1) and PB1-F2 protein (PB1-F2) genes, complete ads Length=2316
...

>gb|KY561069.1| Influenza A virus (A/American green-winged teal/Missouri/15OS6591/2015(H11N9)) segment 1 polymerase PB2 (PB2) gene, complete ads Length=2341
...
  • 2
    How are they to be matched up? Just sequentially? – Kusalananda May 15 '17 at 18:28
  • Yes, sequentially. – ChrisD May 15 '17 at 18:29
  • unclear, FileA should be just replaced with FileB contents as is? – RomanPerekhrest May 15 '17 at 18:41
  • @RomanPerekhrest See edit. – ChrisD May 15 '17 at 18:59
2

One way to do it with awk:

awk 'NR==FNR && /^>Node/ {
    $0 = ""
    for(i=0; i<=4; i++) {
        getline s <ARGV[2]
        $0 = $0 s
    }
}
NR==FNR' FileA FileB
0
perl -lMautodie -pe 'BEGIN{ open FILE_B, "<", pop; }
   s/^>Node.*// && do{for my $k (0..3) { s/$/<FILE_B> =~ s|\n| |r/e }}
' FileA FileB

Explanation

  • Perl options: -l => IFS=OFS=\n, -p => implicit file read in loop + autoprint records.
  • autodie pragma included to bailout on errors.
  • open the FileB
  • for line matching >Node in the beginning of FileA, we read FileB 4 times and removes newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.