I have the following AWK statement in a script:
我在脚本中有以下AWK语句:
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= $minbed) && ($4 >= $minsqft) && ($5 <= $maxprice) && ($6 <= $maxweeks)' $DATFILE | sort -nk5 | less
When I run the script, the output is blank. However, if I run the following:
当我运行脚本时,输出为空。但是,如果我运行以下内容:
grep -E Toronto listing.dat | awk -F "[\t]+" '($3 >= 2) && ($4 >= 500) && ($5 <= 900000) && ($6 <= 10)' listing.dat | sort -nk4 | less
It outputs as expected.
它按预期输出。
I have no idea why this is happening, and I have even replaced the awk statement in the script to echo out the variables to make sure they're passing correctly and they are.
我不知道为什么会发生这种情况,我甚至更换了脚本中的awk语句以回显变量以确保它们正确传递并且它们是。
Here is the script thus far:
这是迄今为止的脚本:
#!/bin/bash
DATFILE=listing.dat
if [ -f ${DATFILE} ];
then
echo -n "Are you looking into anywhere in the GTA, or a specific city?: "
read uinput
if [ $uinput == "anywhere" ];
then
echo "You have chosen anywhere"
elif [ $uinput == "specific" ];
then
echo -n "Which city?: "
read city
echo -n "Minimum Number of Bedrooms: "
read minbed
echo -n "Minimum Square Footage (500, 600, etc): "
read minsqft
echo -n "Maximum Price: "
read maxprice
echo -n "Maximum Weeks On Market: "
read maxweeks
echo -n "Sort by (price, sqrft, weeks): "
read sortby
if [ $sortby == "price" ];
then
echo -n "Sort by (asc, desc): "
read ascdesc
if [ $ascdesc == "asc" ];
then
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= $minbed) && ($4 >= $minsqft) && ($5 <= $maxprice) && ($6 <= $maxweeks)' $DATFILE | sort -nk5 | less
elif [ $ascdesc == "desc" ];
then
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= $minbed) && ($4 >= $minsqft) && ($5 <= $maxprice) && ($6 <= $maxweeks)' $DATFILE | sort -rnk5 | less
fi
fi
fi
else
echo "${DATFILE} Not found!"
fi
Can you please help?
你能帮忙吗?
Thanks
谢谢
2 个解决方案
#1
2
If new to any *nix utility, you can see basic docs using 'man utility_name'; from the man page for 'awk' (man awk):
如果对任何* nix实用程序不熟悉,您可以使用'man utility_name'查看基本文档;来自'awk'(man awk)的手册页:
-v var=val
Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.-v var = val在程序开始执行之前,将值val赋给变量var。这些变量值可用于AWK程序的BEGIN块。
There are other ways to do this, but following the docs your code would be changed to something like:
还有其他方法可以做到这一点,但遵循文档,您的代码将更改为:
awk -F "[\t]+" \
-v MINBED=$minbed -v MINSQFT=$minsqft -v MAXPRICE=$maxprice -v MAXWEEKS=$maxweeks \
'($3 >= MINBED) && ($4 >= MINSQFT) && ($5 <= MAXPRICE) && ($6 <= MAXWEEKS)'
#2
0
I'm not able to comment yet due to reputation. Another thing you can do is escape the single quotes around your variable names. If you're still having trouble with all cities showing up, I believe it's because you're supplying awk with the filename. It looks like you need to take that out and let the grep's output be used as input for your awk. I don't have access to my linux box to test it but:
由于声誉,我无法发表评论。您可以做的另一件事是绕过变量名称的单引号。如果你仍然遇到所有城市出现问题,我相信这是因为你正在为awk提供文件名。看起来您需要将其取出并让grep的输出用作awk的输入。我无法访问我的linux盒来测试它,但是:
your line (having $DATFILE supplied twice):
你的行(提供两次$ DATFILE):
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= $minbed) && ($4 >= $minsqft) && ($5 <= $maxprice) && ($6 <= $maxweeks)' $DATFILE;
option 1: remove the second $DATFILE, escape single quotes
选项1:删除第二个$ DATFILE,转义单引号
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= '$minbed') && ($4 >= '$minsqft') && ($5 <= '$maxprice') && ($6 <= '$maxweeks')';
option 2: you may be able to get rid of the grep entirely, with escaped single quotes. awk's IGNORECASE should be a substitute for grep's -E.
选项2:您可以完全摆脱grep,使用单引号转义。 awk的IGNORECASE应该是grep -E的替代品。
awk -F "[\t]+" 'IGNORECASE = 1;/'$city'/&&($3 >= '$minbed')&&($4 >= '$minsqft')&&($5 <= '$maxprice')&&($6 <= '$maxweeks')';
Like I said, can't test it for syntax at the moment; I hope it helps ya.
就像我说的,目前无法测试它的语法;我希望它能帮助你。
Happy hacking!
快乐的黑客!
#1
2
If new to any *nix utility, you can see basic docs using 'man utility_name'; from the man page for 'awk' (man awk):
如果对任何* nix实用程序不熟悉,您可以使用'man utility_name'查看基本文档;来自'awk'(man awk)的手册页:
-v var=val
Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.-v var = val在程序开始执行之前,将值val赋给变量var。这些变量值可用于AWK程序的BEGIN块。
There are other ways to do this, but following the docs your code would be changed to something like:
还有其他方法可以做到这一点,但遵循文档,您的代码将更改为:
awk -F "[\t]+" \
-v MINBED=$minbed -v MINSQFT=$minsqft -v MAXPRICE=$maxprice -v MAXWEEKS=$maxweeks \
'($3 >= MINBED) && ($4 >= MINSQFT) && ($5 <= MAXPRICE) && ($6 <= MAXWEEKS)'
#2
0
I'm not able to comment yet due to reputation. Another thing you can do is escape the single quotes around your variable names. If you're still having trouble with all cities showing up, I believe it's because you're supplying awk with the filename. It looks like you need to take that out and let the grep's output be used as input for your awk. I don't have access to my linux box to test it but:
由于声誉,我无法发表评论。您可以做的另一件事是绕过变量名称的单引号。如果你仍然遇到所有城市出现问题,我相信这是因为你正在为awk提供文件名。看起来您需要将其取出并让grep的输出用作awk的输入。我无法访问我的linux盒来测试它,但是:
your line (having $DATFILE supplied twice):
你的行(提供两次$ DATFILE):
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= $minbed) && ($4 >= $minsqft) && ($5 <= $maxprice) && ($6 <= $maxweeks)' $DATFILE;
option 1: remove the second $DATFILE, escape single quotes
选项1:删除第二个$ DATFILE,转义单引号
grep -E $city $DATFILE | awk -F "[\t]+" '($3 >= '$minbed') && ($4 >= '$minsqft') && ($5 <= '$maxprice') && ($6 <= '$maxweeks')';
option 2: you may be able to get rid of the grep entirely, with escaped single quotes. awk's IGNORECASE should be a substitute for grep's -E.
选项2:您可以完全摆脱grep,使用单引号转义。 awk的IGNORECASE应该是grep -E的替代品。
awk -F "[\t]+" 'IGNORECASE = 1;/'$city'/&&($3 >= '$minbed')&&($4 >= '$minsqft')&&($5 <= '$maxprice')&&($6 <= '$maxweeks')';
Like I said, can't test it for syntax at the moment; I hope it helps ya.
就像我说的,目前无法测试它的语法;我希望它能帮助你。
Happy hacking!
快乐的黑客!