Issue
NP_001041718.1
XP_021405980.1
NP_001041719.1
XP_021385112.1
NP_001041721.1
XP_021394530.1
NP_001041722.1
XP_021394327.1
NP_001041723.1
XP_021400667.1
I need to capture the matches plus next line in a target file that looks like this, keeping the order from the reference file
NP_001041718.1
DVFMKGLSKAKEGVVAAAEKTKQGVAEAAGKTKEGVLYVGSRTKEGVVHGVTTVAEKTKEQVSNVGGAVVTGVTAVAQKTVEGAGNIAAATGLVKKDQLAKQNEEGFLQEGMVNNTGVAVDPENEAYEMPPEEEYQDYEPEA
NP_001041719.1
GKQNSKLRPEVMQDLLESTDFTEHEIQEWYKGFLRDCPSGHLSMEEFKKIYGNFFPYGDASKFAEHVFRTFDANGDGTIDFREFIIALSVTSRGKLEQKLKWAFSMYDLDGNGYISKSEMLEIVQAIYKMVSSVMKMPEDESTPEKRTEKIFRQMDTNRDGKLSLEEFIRGAKSDPSIVRLLQCDPSSAGQF
NP_001041721.1
TMESGAENQQSGDAAGTEAETQQMTVQAQPQIATLAQVSMPAAHATSSAPTVTLVQLPNGQTVQVHGVIQAAQPSVIQSPQVQTVQISTIAESEDSQESVDSVTDSQKRREILSRRPSYRKILNDLSSDAPGVPRIEEEKSEEETAAPAIATVTVPTPIYQTSSGQYIAITQGGAIQLSNNGTDGVQGLQTLTMTNAAATQPGTTILQYAQTTDGQQILVPSNQVVVQAASGDVQTYQIRTAPTSTIAPGVVMASSPALPTQPAEEAARKREVRLMKNREAARECRRKKKEYVKCLENRVAVLENQNKTLIEELKALKDLYCHKSD
NP_001041722.1
RVNESELNSSVLPRDPPAEGAPRRQPWVTSTLAAILIFTIAVDLLGNLLVILSVYRNKKLRNAGNVFVVSLAVADLIVAIYPYPLVLTSVFHNGWKLGYLHCQISGFLMGLSVIGSIFNITGIAINRYCYICHSLKYDKLYSDRNSLCYIVLIWLLTFVAIVPNLFVGSLQYDPRIYSCTFAQSVSSAYTIAVVFFHFLLPIAVVTFCYLRIWILVIQVRRRVKPDNNPRLKPHDFRNFVTMFVVF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QKTVEGAGNIAAATGLVKKDQLAKQNEEGFLQEGMVNNTGVAVGPENEAYKMPPEEEYQDYEPEA
NP_001041719.1
GKQNSKLRPEVMQDLLESTDFTEHEIQEWYKGFLRDCPSGHLSMEEFKKIYGNFFPYGDASKFAEHVFRTFDANGDGTIDFREFIIALSVTSRGKLEQKLKWAFSMYDLDGNGYISKSEMLEIVQAIYKMVSSVMKMPEDESTPEKRTEKIFRQMDTNRDGKLSLEEFIRGAKSDPSIVRLLQCDPSSAGQF
XP_021385112.1
....
I know how to find the matches in target while keeping the order from ref awk 'FNR==NR {a[$1]=$0; next}; $1 in a {getline} {print a[$1]}' target ref
, but I don't know how to print the line after. I know how to print the line after with grep -A 1 -f ref target
, but it reorders the target file
Solution
Would you please try the following:
awk 'FNR==NR { # process "target" file
if (FNR%2) a[key=$1]=$0 # store odd lines in array a
else b[key]=$0 # store even lines in array b using the same key as the previous line
next
}
$1 in a {print a[$1]; print b[$1]} # if the key matches, print the odd line and the even line
' target ref
Answered By - tshiono Answer Checked By - Clifford M. (WPSolving Volunteer)