我正在阅读的文件样本
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
013160,32.8347,-88.1342, 38.1 AL GAINESVILLE LOCK
013511,32.7017,-87.5808, 67.1 AL GREENSBORO
013816,31.8700,-86.2542, 132.0 AL HIGHLAND HOME
015749,34.7442,-87.5997, 164.6 AL MUSCLE SHOALS AP
017157,34.1736,-86.8133, 243.8 AL SAINT BERNARD
017304,34.6736,-86.0536, 187.5 AL SCOTTSBORO
GAWK代码
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print $1,$2,$3,$4
station=""$1 #Forces to be string
#Save latitude
stationInfo[station][lat]=$2
print "lat",stationInfo[station][lat]
#Save longitude
stationInfo[station][lon]=$3
print "lon",stationInfo[station][lon]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station][lat]
print "---------------"
}
示例输出
011084,31.0581,-87.0547, 25.9 AL BREWTON 3 SSE
lat,31.0581
lon,-87.0547
lat,-87.0547
---------------
012813,30.5467,-87.8808, 7.0 AL FAIRHOPE 2 NE
lat,30.5467
lon,-87.8808
lat,-87.8808
---------------
由于某种原因,stationInfo[station][lat]
中存储的值被经度覆盖。我对世界上正在发生的事情感到茫然。
我在Fedora 22上使用GAWK 4.1.1
答案 0 :(得分:0)
您的问题是lon
和lat
是变量并评估为空字符串,因此此作业stationInfo[station][lat]=$2
和stationInfo[station][lon]=$3
分配给stationInfo[station]["]
您需要引用这些(和其他)行中的lat
和lon
来使用字符串而不是变量。
#!/bin/gawk
BEGIN{
FS=",";
OFS=",";
}
{
print $1,$2,$3,$4
station=""$1 #Forces to be string
#Save latitude
stationInfo[station]["lat"]=$2
print "lat",stationInfo[station]["lat"]
#Save longitude
stationInfo[station]["lon"]=$3
print "lon",stationInfo[station]["lon"]
#Now try printing the latitude again
#It will return the value of the longitude instead
print "lat",stationInfo[station]["lat"]
print "---------------"
}