gpt4 book ai didi

linux - 在 Unix while-do 循环中,比较条件失败

转载 作者:太空宇宙 更新时间:2023-11-04 05:27:46 25 4
gpt4 key购买 nike

输入文件是:

file1 的架构是:

offer rank score lat lon eligiblecms

文件1:

10000,1,0.0,"-31.940742,115.86829",3760006987500
12345,2,0.0,"-31.940742,115.86829",3760006987500
13245,3,0.0,"-31.940742,115.86829",3760006987500
11111,4,0.0,"-31.940742,115.86829",3760006987500
11112,5,0.0,"-31.940742,115.86829",3760006987500

文件 2 的架构为:

offer1 rank1 score1 lat1 lon1 eligiblecms1

文件2

10000,1,0.0,"-31.940742,115.86829",3760006987500
12345,2,0.0,"-31.940742,115.86829",3760006987500
13245,3,0.0,"-31.940742,115.86829",3760006987500
11111,4,0.0,"-31.940742,115.86829",3760006987500
11112,5,0.0,"-31.940742,115.86829",3760006987500
32152,6,0.0,"-31.940742,115.86829",3760006987500
32153,7,0.0,"-31.940742,115.86829",3760006987500
32154,8,0.0,"-31.940742,115.86829",3760006987500
32163,9,0.0,"-31.940742,115.86829",3760006987500
32164,10,0.0,"-31.940742,115.86829",3760006987500
32165,11,0.0,"-31.940742,115.86829",3760006987500
32167,12,0.0,"-31.940742,115.86829",3760006987500
32170,13,0.0,"-31.940742,115.86829",3760006987500
32171,14,0.0,"-31.940742,115.86829",3760006987500
32182,15,0.0,"-31.940742,115.86829",3760006987500
32183,16,0.0,"-31.940742,115.86829",3760006987500

我的代码为:

#!/bin/bash
while IFS=,
read offer rank score lat lon eligiblecms
#dataflag="F"
do
while IFS=,
read offer1 rank1 score1 lat1 lon1 eligiblecms1
do
if [ "$offer" = "$offer1" ]
then
echo "Details of Offer $offer :"
if [ "$lat" = "$lat1" ] && [ "$lon" = "$lon1" ]
then
echo "Expected latlong "$lat", "$lon" successfully matched with the Actual Latlong: "$lat1,$lon1" "
else
echo " Expected latlong "$lat","$lon" did not matched with the Latlong of API output offer "$offer1" "
fi
if [ "$eligiblecms" = "$eligiblecms1" ]
then
echo " Expected UID "$eligiblecms" successfully matched with the UID of API output offer "$eligiblecms1" "
else

echo " Expected UID "$eligiblecms" did not matched with UID of API output offer "$offer1" "
fi
fi
done <$1
#if [ "$dataflag" = "F" ]
#then
#echo ""$offer" not found"
#fi
done <$2

其输出是:

 Details of Offer 10000   :
Expected latlong "-31.940742, 115.86829" successfully matched with the Actual Latlong: "-31.940742,115.86829"

Expected UID 3760006987500 did not matched with UID of API output offer 10000
Details of Offer 12345 :
Expected latlong "-31.940742, 115.86829" successfully matched with the Actual Latlong: "-31.940742,115.86829"

Expected UID 3760006987500 did not matched with UID of API output offer 12345
Details of Offer 13245 :
Expected latlong "-31.940742, 115.86829" successfully matched with the Actual Latlong: "-31.940742,115.86829"

Expected UID 3760006987500 did not matched with UID of API output offer 13245
Details of Offer 11111 :
Expected latlong "-31.940742, 115.86829" successfully matched with the Actual Latlong: "-31.940742,115.86829"

Expected UID 3760006987500 did not matched with UID of API output offer 11111
Details of Offer 11112 :
Expected latlong "-31.940742, 115.86829" successfully matched with the Actual Latlong: "-31.940742,115.86829"

Expected UID 3760006987500 did not matched with UID of API output offer 11112

我不确定为什么我无法随时为我的代码获取指定的条件,按照我的逻辑它应该是true。

"$eligiblecms" = "$eligiblecms1"

我得到的输出为:

Expected UID 3760006987500 did not  matched with UID of API output offer 11112

根据我的逻辑输出应该是:

Expected UID "$eligiblecms" successfully  matched with the UID of API output offer "$eligiblecms1

请帮忙。

最佳答案

这是编写脚本的正确方法:

$ cat tst.awk                
BEGIN {
FS = "[,\"]+"
offer = ++enum
rank = ++enum
score = ++enum
lat = ++enum
lon = ++enum
eligiblecms = ++enum
}

NR==FNR { file1[$offer] = $0; next }
{
if ( $offer in file1 ) {
split(file1[$offer],expd)
split($0,act)

print "Details of Offer " act[offer] " :"

if ( (act[lat] == expd[lat]) && (act[lon] == expd[lon]) ) {
print "Expected latlong " expd[lat] ", " expd[lon] " successfully matched with the Actual Latlong: " act[lat] "," act[lon]" "
}
else {
print " Expected latlong " expd[lat] "," expd[lon] " did not matched with the Latlong of API output offer " act[offer] " "
}

if ( act[eligiblecms] == expd[eligiblecms] ) {
print " Expected UID " expd[eligiblecms] " successfully matched with the UID of API output offer " act[eligiblecms] " "
}
else {
print " Expected UID " expd[eligiblecms] " did not matched with UID of API output offer " act[offer] " "
}
}
else {
print $offer, "not found"
}
}

.

$ awk -f tst.awk file1 file2
Details of Offer 10000 :
Expected latlong -31.940742, 115.86829 successfully matched with the Actual Latlong: -31.940742,115.86829
Expected UID 3760006987500 successfully matched with the UID of API output offer 3760006987500
Details of Offer 12345 :
Expected latlong -31.940742, 115.86829 successfully matched with the Actual Latlong: -31.940742,115.86829
Expected UID 3760006987500 successfully matched with the UID of API output offer 3760006987500
Details of Offer 13245 :
Expected latlong -31.940742, 115.86829 successfully matched with the Actual Latlong: -31.940742,115.86829
Expected UID 3760006987500 successfully matched with the UID of API output offer 3760006987500
Details of Offer 11111 :
Expected latlong -31.940742, 115.86829 successfully matched with the Actual Latlong: -31.940742,115.86829
Expected UID 3760006987500 successfully matched with the UID of API output offer 3760006987500
Details of Offer 11112 :
Expected latlong -31.940742, 115.86829 successfully matched with the Actual Latlong: -31.940742,115.86829
Expected UID 3760006987500 successfully matched with the UID of API output offer 3760006987500
32152 not found
32153 not found
32154 not found
32163 not found
32164 not found
32165 not found
32167 not found
32170 not found
32171 not found
32182 not found
32183 not found
$

idk 我设法正确地复制了您的逻辑,但希望它足够清晰,您可以在必要时对其进行调整。我尝试使用与您已经使用的语法尽可能相似的语法,因此如果您不了解 awk,那么最容易理解。

关于linux - 在 Unix while-do 循环中,比较条件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28628117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com