I am new to sed and would like to know how to replace a pattern with a different variable every time
I have a txt file that looks like this:
@K3KFV:1:1109:11598:25872 @K3KFV:1:2101:22577:15247 @K3KFV:1:1110:13477:13178 @K3KFV:1:2113:23585:6859... (etc)
In total there are 200 different lines. In addition I have another file:
ASF356_KB822565.1:1065516-1065795 TAGGTCAAGCCCTCGGTCTATTAGTATTGGTCAGCTTAATACATTGCTGCACTTACACCT CCAACCTATCTACCTTGTTGTCTTCAAGGGACCTTACTCACTTGCGTTTTGGGATATCTT ASF356_KB822565.1:1065796-1066075 CGGATAGGGACCGAACTGTCTCACGACGTTCTGAACCCAGCTCGCGTACCGCTTTAATGG GCGAACAGCCCAACCCTTGGGACCTACTTCAGCCCCAGGATGCGATGAGCCGACATCGAG ASF356_KB822565.1:1066076-1066355 CCTTTTGCCTTTACACTCTTTGAATGGTTTCCAATCATTCTGAGGTGACCTTCGAGCGCC TCCGTTACTCTTTTGGAGGCGACCGCCCCAGTCAAACTGCCCGCCTGACATTGTCCTTCA
which also contains 200 instances of "ASF....."
What I want is to replace the line containing "ASF..." with one from "@K3KFV:....." so in the end it will look like:
@K3KFV:1:1109:11598:25872 TAGGTCAAGCCCTCGGTCTATTAGTATTGGTCAGCTTAATACATTGCTGCACTTACACCT CCAACCTATCTACCTTGTTGTCTTCAAGGGACCTTACTCACTTGCGTTTTGGGATATCTT @K3KFV:1:2101:22577:15247 CGGATAGGGACCGAACTGTCTCACGACGTTCTGAACCCAGCTCGCGTACCGCTTTAATGG GCGAACAGCCCAACCCTTGGGACCTACTTCAGCCCCAGGATGCGATGAGCCGACATCGAG @K3KFV:1:1110:13477:13178 CCTTTTGCCTTTACACTCTTTGAATGGTTTCCAATCATTCTGAGGTGACCTTCGAGCGCC TCCGTTACTCTTTTGGAGGCGACCGCCCCAGTCAAACTGCCCGCCTGACATTGTCCTTCA
This is the shell script I have so far:
input="K3KFVfile.txt" while IFS= read -r title do sed '/ASF/c'$title'' ASF_file done < "$input"
But instead of giving me 200 lines of @K3KFV... I get 40000 because each ASF line gets replaced with every single one of the @K3KFV lines.
Is there a way to use sed to replace a pattern using a variable only once before moving on? Is sed the correct command to use in this case?