Awk: check for value in array

I am trying to read two separate awk files and parse the second into an output file.

file1 contains the numbers:

1 2 5 7 10 

file2 contains a header (number of fields <3) and data values ​​in columns (25 columns)

 _rlnNrOfSignificantSamples #24 _rlnMaxValueProbDistribution #25 300.000000 25425.970703 25000.669922 6.050000 2.000000 56.000000 0.277790 79096.000000 0.100000 000001@Particles /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 000002@Particles /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 

I want to read the numbers from file1 into an array, and then:

  • print header from file2
  • print lines from file2 if the values ​​in the $ 22 field are NOT in the array (in the example earlier, its values ​​are 23 and 22)

After one day of struggle, I came up with the following:

 #!/bin/bash FieldNum=22 awk -vf=$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 

But it turns out that all lines from file2 are printed, so checking the array does not work. Could you please indicate my mistake?

+4
source share
1 answer

this single line file should do what you want:

  awk 'NR==FNR{a[$0];next}NF<3||!($22 in a)' file1 file2 

Your problem is that you have var f , which is a number, I think it is a column index.

but if you check your code, you used f as the value, check if f in the array, instead of checking $f

That is, if you gave f=22 , for each line in file2 you check if the constant is 22 in the array. Thus, the result will be either all the lines in file2, or only the headers in file2, it depends on whether there will be a constant 22 in your file1. :)

+12
source

Source: https://habr.com/ru/post/1494728/


All Articles