Alexis Purslane
Alexis Purslane

Reputation: 1390

AWK dictionary not storing value between lines

I'm trying to deduplicate the output of an awk script by storing each word seen as the key in a dictionary, with the number of times it's been seen as the value, and only printing the word if the dictionary was zero before the increment. E.g.:

match($0, /^(\w+)$/, a) { if (!$seen[a[1]]++) print a[1], "new word" }

however, this doesn't seem to work. The value of $seen[a[1]] is always zero, even if the output of the print statement shows the exact same word. I've been stuck on this for awhile now. What am I missing? Thank you!

Upvotes: 0

Views: 46

Answers (1)

Alexis Purslane
Alexis Purslane

Reputation: 1390

Thanks to @markp-fuso for providing the answer: unlike in Bash, in Awk, you can access a variable without a dollar sign in front of it, so $seen[a[1]] actually refers to the column corresponding to whatever the value of seen[a[1]] is (e.g. if seen[a[1]] == 0, then $seen[a[1]] == $0 and so on), so I just needed to reference it without the dollar sign.

Upvotes: 1

Related Questions