AWK不bash脚本工作 [英] AWK doesn't work in Bash Script
问题描述
我有一个脚本以下AWK语句:
I have the following AWK statement in a script:
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
据产出预期。
我不知道为什么发生这种情况,我甚至已经取代了脚本回声出变量awk的语句,以确保他们正确传递,他们是。
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.
下面是脚本迄今:
#!/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
您可以请帮助?
感谢
推荐答案
如果新的* nix的工具,你可以看到使用人utility_name的基本文档;从'awk的'(AWK人)的手册页:
If new to any *nix utility, you can see basic docs using 'man utility_name'; from the man page for 'awk' (man awk):
-v VAR = VAL结果
分配值VAL到变量var,程序的执行开始之前。这样的变量值可用来BEGIN块
一个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.
有其他方法可以做到这一点,但在之后的文档code将改为类似:
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)'
这篇关于AWK不bash脚本工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!