AWK:在另一个产生语法错误的文本文件中搜索文件名

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

我有一个包含文件名列表的文本文件 (file_list.txt) (例如 contour_0.6_266_midpoints_2_0_0.dat).

我有第二个文件($iw_fitted_midpoints_file.dat),其中包含这些文件名,后跟一系列参数和 x、y 信息。 $iw_fitted_midpoints_file.dat 的前 10 行是:

> -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 

我正在尝试为每个文件名搜索第二个文件($iw_fitted_midpoints_file.dat)并提取相关行的第 32 列。我的问题是我无法获得转义“.”的语法。文件名正确。

这是我的代码

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

这是我的错误

awk: {if ( == ">" &&  ~ contour_0.6_96_midpoints_16_0_0.dat) print [=13=]}
awk:                                                       ^ syntax error

例如,如果我正在搜索 contour_0.6_266_midpoints_2_0_0.dat 上面的代码会给我 K=-1.573416(即 $iw_fitted_midpoints_file.dat 中第一行的第 32 列)

不用转义特殊字符,只需进行精确匹配,将值作为 awk 变量传递

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