Text-Processing
用另一個文件的輸出查找和替換文本文件中的行
我有兩個文件 A 和 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
如何用文件 B 中的後續條目替換文件 A 的每個 NodeX 行?結果如下所示:
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 ...
一種方法
awk
:awk 'NR==FNR && /^>Node/ { $0 = "" for(i=0; i<=4; i++) { getline s <ARGV[2] $0 = $0 s } } NR==FNR' FileA FileB
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
解釋
- Perl 選項:
-l
=>IFS=OFS=\n
,-p
=> 循環中讀取的隱式文件 + 自動列印記錄。autodie
包括編譯指示以救助錯誤。- 打開文件B
- 對於 FileA 開頭的行匹配 >Node,我們讀取 FileB 4 次並刪除換行符。