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

时间:2015-02-20 11:40:43

标签: linux bash shell unix 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 <$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

我不确定为什么我无法在任何时候获得我的代码的指定条件,它应该按照我的逻辑。

"$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

请帮忙。

1 个答案:

答案 0 :(得分:0)

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

$ 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,最容易理解。