[英]Awk not working in bash script when use variable in comparison
Following is my bash script. 以下是我的bash脚本。 If I use varible oid to compare in awk, it doesnt show matching line. 如果我使用varible oid在awk中进行比较,它不会显示匹配的行。
oid="3586302804992"
SYMBOL_CSV_FILE="symbol/BAC"
awk -F, '$5 == $oid' "$SYMBOL_CSV_FILE"
echo "2nd"
awk -F, '$5 == "3586302804992"' "$SYMBOL_CSV_FILE"
O/P is O / P是
2nd
BAC,1,O,1,3586302804992
symbol/BAK
file contents are symbol/BAK
文件内容是
BAC,1,O,1,3586302804992o
Putting "" around $oid , on 3rd line, doesnt make any difference. 将“”放在第3行的$ oid附近,并没有任何区别。
Instead of: 代替:
awk -F, '$5 == $oid' "$SYMBOL_CSV_FILE"
use it like this: 像这样用它:
awk -F "," -v oid="$oid" '$5 == oid' "$SYMBOL_CSV_FILE"
For bash to interpret your variables, you have to use the double quotes. 要让bash解释您的变量,您必须使用双引号。 Single quotes will send $oid
as is to your program. 单引号会将$oid
发送给您的程序。
Then, as the $5
will also be interpreted, and you don't want to! 然后,因为$5
也将被解释,而你不想! You have to escape the $
. 你必须逃避$
。
In the end, you have: 最后,你有:
awk -F, "\$5 == $oid" "$SYMBOL_CSV_FILE"
^^ ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.