Checking upper case in AWK with range [A-Z] not working

awk, bash, regex

Solution

It's your locale setting. Google locale. Set LC_ALL=C to get a common one but using character classes is the right approach.

Problem

I have the following bash script: ``` cat test | awk '/^[A-Z]/ {print NF}' ``` Let's assume the content of test is the following: ``` This one yes this one no This one yes this one no ``` In principle it should print the number of fields of first and third line, since I'm looking for lines beginning with a character in upper case. However, the result is that number of fields of all the lines in the file is printed. If I try with grep, with the same regular expression, the result is what I expect. In addition, if instead of putting [A-Z], I write [[:upper:]] the script works flawlessly. My question is: why in the first case it does not work?

Original source