Kevin Penner
Kevin Penner

Reputation: 21

using grep to find filenames with accented characters or ñ in the search term

I'm trying unsuccessfully to use (e)grep to list files in a directory when my search term includes Spanish characters such as accented vowels or "ñ" (e.g. bebé, caña). In the case of accented vowels, if I use the unaccented vowel, the search works as in the following:

$ ls | egrep 'bebe'
8 bebé
IMQ_bebé1.wav
IMQ_bebé2.wav
IMQ_bebé3.wav
IMQ_bebé4.wav
IMQ_bebé5.wav
IMQ_bebé6.wav

However, if I include the accented vowel in the search term, (e.g. $ ls | egrep 'bebé') I get nothing.

The reason why using non-accented vowels in the search term is not possible is because this is part of a larger shell script that gets it's search terms from a text file. Also, search terms with "ñ" don't work, nor do they work with just "n". Plus, I'm sure there must be a way to do this!

I'm working on Mac OS X 10.6.8. My locale is as follows:

$ locale
LANG="en_CA.UTF-8"
LC_COLLATE="en_CA.UTF-8"
LC_CTYPE="en_CA.UTF-8"
LC_MESSAGES="en_CA.UTF-8"
LC_MONETARY="en_CA.UTF-8"
LC_NUMERIC="en_CA.UTF-8"
LC_TIME="en_CA.UTF-8"
LC_ALL=

Upvotes: 2

Views: 1068

Answers (1)

perreal
perreal

Reputation: 97948

You can use iconv to unaccent your list and match ascii patterns:

ls | iconv -f utf8 -t ascii//TRANSLIT | egrep 'bebe'

Upvotes: 1

Related Questions