J'ai modifié le code pour illustrer le contenu de
freq à chaque itération:
Code:
$cat freq.awk
#! /bin/ksh
awk '
# Print list of word frequencies
{
for (i = 1; i <= NF; i++)
{
# Here loop variable i will contain values like 1, 2, 3 until last field of
# Record which in your case is 5, so $i will behave like $1, $2, $3
# Following condition will lookup in array if key($i ie name of city in your case)
# Already exists in array or not
if ( $i in freq )
print "Key(" $i "): is already there in array and its frequency or value is: " freq[$i]
# In the 1st iteration of loop, $i($1) is equal to Kuwait, so freq[$i]
# Means freq[Kuwait], because you dont have any value associated to key Kuwait
# Therefor freq[$i] yeilds to zero and ++ operator increases it to 1
freq[$i]++
# Print the record no., field no. value of key and freq associated to key, at each iteration of loop
print "Record No." NR "\tField No: " i "\tValue of key: " $i "\tFrequency of word: " freq[$i]
}
print "\n"
}
END {
# Loop thru the whole array(freq) and find the associated value to each key
# Here word denotes key and freq[word] refres value associated to each key
for (word in freq)
printf "%s\t%d\n", word, freq[word]
}' abc.txt