This question already has an answer here:

I have got the following awk script from here that counts the frequency of the words in a file-:

  # Print list of word frequencies
    for (i = 1; i <= NF; i++)

    for (word in freq)
        printf "%s\t%d\n", word, freq[word]

What really makes no sense to me is how the for loop works when it is counting the words from the file. In the loop variable i is initialized to an integer number 1 and checks if it is less that the record of NF (which holds the maximum number of fields per record).

But when I am assigning i to the array I am assigning an actual word (or string).

So this means that i is both a string and a number ? How is that even possible ?

marked as duplicate by cuonglm, jasonwryan, Anthon, Braiam, slm Nov 23 '14 at 21:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    i is a number (integer), but $i is a string which is present on ith field position. – jimmij Nov 23 '14 at 13:22
  • @cuonglm This question is about how the loop variables work in awk. The other question is about the behavior of variables in general. – Sreyan Nov 23 '14 at 17:22
  • @Sreyan: No, you just confused about i and $i. – cuonglm Nov 23 '14 at 17:23

Browse other questions tagged or ask your own question.