findstr or grep that autodetects chararacter encoding (UTF-16)
Asked Answered
O

7

13

I want to do this:

 findstr /s /c:some-symbol *

or the grep equivalent

 grep -R some-symbol *

but I need the utility to autodetect files encoded in UTF-16 (and friends) and search them appropriately. My files even have the byte-ordering mark FFEE in them so I'm not even looking for heroic autodetection.

Any suggestions?


I'm referring to Windows Vista and XP.

Obligate answered 2/1, 2009 at 21:28 Comment(2)
Are some of your files in UTF-16 and some in ASCII, or what?Sallust
Yep, some ASCII, some UTF-16.Obligate
H
5

A workaround is to convert your UTF-16 to ASCII or ANSI

TYPE UTF-16.txt > ASCII.txt

Then you can use FINDSTR.

FINDSTR object ASCII.txt
Hubblebubble answered 4/9, 2012 at 15:58 Comment(2)
...pardon, what?Eagre
or type UTF-16.txt | findstr if you don't need filename (OP needs filename because searching multiple files, but some may find this useful)Mangan
O
4

Thanks for the suggestions. I was referring to Windows Vista and XP.

I also discovered this workaround, using free Sysinternals strings.exe:

C:\> strings -s -b dir_tree_to_search | grep regexp 

Strings.exe extracts all of the strings it finds (from binaries, but works fine with text files too) and prepends each result with a filename and colon, so take that into account in the regexp (or use cut or another step in the pipeline). The -s makes it do a recursive extraction and -b just suppresses the banner message.

Ultimately I'm still kind of surprised that the flagship searching utilities Gnu grep and findstr don't handle Unicode character encodings natively.

Obligate answered 4/1, 2009 at 13:55 Comment(2)
On their home unix environments, UTF-16 is much less common, and files are generally in UTF-8, which they handle just fine.Douce
Maybe not so great for extracting the whole line, but perfect for trying to find all files containing a string (which I'm trying to do). Thanks.Fen
W
3

On Windows, you can also use find.exe.

find /i /n "YourSearchString" *.*

The only problem is this prints file names followed by matches. You may filter them by piping to findstr

find /i /n "YourSearchString" *.* | findstr /i "YourSearchString"
Woosley answered 21/7, 2011 at 20:31 Comment(1)
Unfortunately find command doesn't support matching patterns like findstr (wildcards / regular expressions).Adela
H
3
findstr /s /c:some-symbol *

can be replaced with the following character encoding aware command:

for /r %f in (*) do @find /i /n "some-symbol" "%f"
Hegel answered 9/1, 2013 at 19:23 Comment(1)
If add Venkateshwar's answer below, you get: for /r %f in (*) do @find /i /n "some-symbol" "%f" | findstr /i "some-symbol" which will filter out the filenames. I found this useful when searching a set of files looking for "Fail". I didn't care what file it appeared in, I just wanted to see if any file had "Fail" in it.Dalpe
K
1

According to this blog article by Damon Cortesi grep doesn't work with UTF-16 files, as you found out. However, it presents this work-around:

for f in `find . -type f | xargs -I {} file {} | grep UTF-16 | cut -f1 -d\:`
        do iconv -f UTF-16 -t UTF-8 $f | grep -iH --label=$f ${GREP_FOR}
done

This is obviously for Unix, not sure what the equivalent on Windows would be. The author of that article also provides a shell-script to do the above that you can find on github here.

This only greps files that are UTF-16. You'd also grep your ASCII files the normal way.

Klansman answered 2/1, 2009 at 22:22 Comment(0)
E
1

In higher versions of Windows, UTF-16 is supported out-of-box. If not, try changing active code page by chcp command.

In my case when using findstr alone was failing for UTF-16 files, however it worked with type:

type *.* | findstr /s /c:some-symbol
Erectile answered 24/1, 2017 at 22:46 Comment(0)
D
0

You didn't say which platform you want to do this on.

On Windows, you could use PowerGREP, which automatically detects Unicode files that start with a byte order mark. (There's also an option to auto-detect files without a BOM. The auto-detection is very reliable for UTF-8, but limited for UTF-16.)

Dawdle answered 3/1, 2009 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.