9

I am trying to find all 6 letter words using grep. I currently have this:

grep "^.\{6\}$" myfile.txt 

However, I am finding that I am also getting results such as: étuis, étude.

I suspect it has something do with the symbols above the e in the above words.

Is there something I can do to ensure that this does not happen?

Thanks for your help!

4

grep's idea of a character is locale-dependent. If you're in a non-Unicode locale and you grep from a file with Unicode characters in it then the character counts won't match up. If you echo $LANG then you'll see the locale you're in.

If you set the LC_CTYPE and/or LANG environment variables to a value ending with ".UTF-8" then you will get the right behaviour:

$ cat data
étuis
letter
éééééé
$ LANG=C grep -E '^.{6}$' data
étuis
letter
$ LANG=en_US.UTF_8 grep -E '^.{6}$' data
letter
éééééé
$

You can change your locale for just a single command by assigning the variable on the same line as the command.

With this configuration, multi-byte characters are considered as single characters. If you want to exclude non-ASCII characters entirely, some of the other answers have solutions for you.


Note that it's still possible for things to break, or at least not do exactly what you expect, in the presence of combining characters. Your grep may treat LATIN SMALL LETTER E + COMBINING CHARACTER ACUTE ABOVE differently than LATIN SMALL LETTER E WITH ACUTE.

  • if you use ., something like wăsd's will match – cuonglm Jul 31 '14 at 5:07
  • ' is a character that can reasonably be part of a "string with a fixed number of characters". – Michael Homer Jul 31 '14 at 5:19
  • Maybe. And you should set both LC_CTYPE and LANG, something like LC_CTYPE=en_US.UTF-8 LANG=en_US will be failed. Use LC_ALL for safety. – cuonglm Jul 31 '14 at 5:41
2

Try this:

LC_ALL=C.UTF-8 grep -x '[_[:alnum:]]\{6\}' file

-x use to match whole line, and defined by POSIX (See grep).

See here for good explanation of what LC_ALL does. You can set LANG or LC_CTYPE to use utf-8 to get the same behaviour. The order taking affect is LC_ALL => LANG => LC_CTYPE.

2

With GNU grep when built with PCRE support, you can do:

grep -Px '\X{6}'

While . matches a character, \X matches an ideogram/graphem.

In a UTF-8 locale:

$ locale charmap
UTF-8
$ printf '\u00e9tuis\n\u00e9tudes\n' | grep -Px '\X{6}'
études
$ printf 'e\u0301tuis\ne\u0301tudes\n' | grep -Px '\X{6}'
études

In that latter études, there are 7 characters, 8 bytes, and 6 graphems.

  • It seems does not work: echo épée | grep -Px '\X{6}' ouput épée – cuonglm Jul 31 '14 at 9:46
  • @Gnouc, you need to run that in a UTF-8 locale (if those é above were encoded in UTF-8). – Stéphane Chazelas Jul 31 '14 at 9:47
  • Oh, my mistakes. It works with UTF-8. – cuonglm Jul 31 '14 at 9:50
0

You could try something like:

grep "^[A-Za-z]\{6\}$" myfile.txt

or if the words may contain numbers too, then:

grep "^[A-Za-z0-9]\{6\}$" myfile.txt

Just add any characters to the square brackets that you want in addition to these.

  • This won't match the étude at all, because the ASCII character corresponding to the accent will mess up the regex. – Alex Aug 23 '17 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy