Search code examples
awkescapingspecial-characters

AWK: search for filename within another text file producing syntax error


I have a text file (file_list.txt) that contains a list of filenames (e.g. contour_0.6_266_midpoints_2_0_0.dat).

I have a second file ($iw_fitted_midpoints_file.dat) which contains these filenames followed by a series of parameters and x, y information. The first 10 lines of $iw_fitted_midpoints_file.dat are:

> -Z contour_0.6_266_midpoints_2_0_0.dat 0.125512 96 0.009539 0.000017 1524.160000 1.000000 97.000000 44.000000 4 6 0.670000 0.214029 0.214030 -1.180430 -0.636157 1.307050 -0.647329 -1.101411 36.000000 -0.647530 0.145132 -1.400605 0.219184 30.949074 0.670000 48.602367 0.200000 -4.915458 -1.573416 0.428059 3376.000000
3333.000000 0.105569 
3334.000000 0.106486 
3335.000000 0.107501 
3336.000000 0.109010 
3337.000000 0.110718 
3338.000000 0.107739 
3339.000000 0.107382 
3340.000000 0.106499 
3341.000000 0.106408 

I am trying to search the second file ($iw_fitted_midpoints_file.dat) for each filename and extract column 32 of the relevant line. My problem is that I cannot get the syntax for escaping the "." in the filenames correct.

This is my code

for f in `cat file_list.txt`; do

name=$(echo "$f" | cut -f 1,2,3 -d '.')

K=awk '{if ($1 == ">" && $2 ~ '\${name}') print $0}'$iw_fitted_midpoints_file.dat

echo $K



  done

And this is my error

awk: {if ($1 == ">" && $2 ~ contour_0.6_96_midpoints_16_0_0.dat) print $0}
awk:                                                       ^ syntax error

For example, if I was searching for contour_0.6_266_midpoints_2_0_0.dat the above code would give me K=-1.573416 (i.e. the 32nd column from the first line in $iw_fitted_midpoints_file.dat)


Solution

  • instead of escaping special chars, just do an exact match, pass the value as an awk variable

    $ awk -v name="$f" '$1==">" && $2==name {print $32}' datafile