4
我想通過awk讀取兩個單獨的文件,並將第二個文件解析爲輸出文件。Awk:檢查值是否不在數組中
file1的包含數字:
1
2
5
7
10
file2中包含一個標題和數據值在列
_rlnNrOfSignificantSamples #24
_rlnMaxValueProbDistribution #25
300.000000 25425.970703 25000.669922 6.050000 2.000000 56.000000 0.277790 79096.000000 0.100000 [email protected]/Micrographs/006_particles.mrcs 453.000000 604.000000 1.000000 0.859382 Micrographs/006.mrc 1 -3.469177 -3.469177 0.000000 0.000000 -82.345885 23 9475.876495 1 0.988689
300.000000 25425.970703 25000.669922 6.050000 2.000000 56.000000 0.277790 79096.000000 0.100000 [email protected]/Micrographs/006_particles.mrcs 431.000000 428.000000 1.000000 0.806442 Micrographs/006.mrc 1 -1.469177 -3.469177 0.000000 0.000000 87.654115 22 9412.959278 1 1.000000
(字段< 3的數)(25列)我想從文件1的數字讀入array,則:
- 打印頭文件2 從文件個
- 打印行,如果字段值$ 22的陣列不是(例如在早期其值是23和22)
掙扎的一天後,我想出了以下內容:
#!/bin/bash
FieldNum=22
awk -v f=$FieldNum 'FNR==NR{num[$1]; next}
{
# print the header of file2
if(NF < 3) {print > "output"}
# check lines after header
else {if (f in num) {} else {print >> "output"}}
}' $file1 $file2
但事實證明,從file2打印所有行,所以數組檢查不起作用。你能否發現我的錯誤?
非常感謝你!我忘記了默認情況下,awk檢查數組中是否存在特定的索引,而不是搜索該值。 –