Reputation: 1753
I know awk has IGNORECASE to make the operation case insensitive. But I am not able to figure out how to use it in a for loop. For example: Consider this awk script:
{
for (i = 1; i <= NF; i++)
counter[$i]++
}
Here I know I can use tolower, but what if I want to do it with IGNORECASE = 1, so that it ignores case while counting.
Upvotes: 0
Views: 378
Reputation: 3047
tolower seems the way to go. Look here for further info:
"In general, you cannot use IGNORECASE to make certain rules case-insensitive and other rules case-sensitive, because there is no straightforward way to set IGNORECASE just for the pattern of a particular rule.17 To do this, use either bracket expressions or tolower(). However, one thing you can do with IGNORECASE only is dynamically turn case-sensitivity on or off for all the rules at once."
from: The GNU Awk User's Guide
EDIT:
You should think about a better specification of your problem. Try this:
#Input: hello Hello HELLO World
#Output: hello 3 World 1
{for (i=1;i<=NF;i++){
a=tolower($i)
count[a]=$i","count[a]
}
}
END{for (i in count){
split(count[i],res,",")
l=length(res) - 1
if (l==1){
print res[1],1
} else {
print tolower(res[1]),l}
}
}
HTH Chris
Upvotes: 1