我有一个脚本以下AWK语句工作。但是,如果我运行以下操作:AWK不bash脚本
grep -E Toronto listing.dat | awk -F "[\t]+" '($3 >= 2) && ($4 >= 500) && ($5 <= 900000) && ($6 <= 10)' listing.dat | sort -nk4 | less
它按预期输出。
我不知道为什么会发生这种情况,我甚至已经替换了脚本中的awk语句来回显变量以确保它们正确传递并且它们是。
这是迄今为止该脚本:
#!/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
能否请你帮忙吗?
感谢
'gawk'或'awk'?你的例子中都有。 – miken32
您的awk脚本使用单引号。当它们位于引号引号内时,Shell变量不会被展开。所以您需要使用-v将它们分配给awk变量。 –
无论如何,请勿将grep结果传递给awk。 awk本身能够通过正则表达式来过滤 – hek2mgl