在 Unix while-do 循环中比较条件失败
In Unix while-do loop the comparision condition is failing
输入文件是:
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 <
#if [ "$dataflag" = "F" ]
#then
#echo ""$offer" not found"
#fi
done <
谁的输出是:
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
我不确定为什么我无法随时为我的代码获取指定的条件 ture,按照我的逻辑应该是 ture。
"$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] = [=10=]; next }
{
if ( $offer in file1 ) {
split(file1[$offer],expd)
split([=10=],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 也最容易理解。
输入文件是:
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 <
#if [ "$dataflag" = "F" ]
#then
#echo ""$offer" not found"
#fi
done <
谁的输出是:
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
我不确定为什么我无法随时为我的代码获取指定的条件 ture,按照我的逻辑应该是 ture。
"$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] = [=10=]; next }
{
if ( $offer in file1 ) {
split(file1[$offer],expd)
split([=10=],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 也最容易理解。