findstr或grep自动检测字符编码(UTF-16)

Dav*_*tin 12 unicode windows-xp utf-16 findstr windows-vista

我想做这个:

 findstr /s /c:some-symbol *
Run Code Online (Sandbox Code Playgroud)

或grep等价物

 grep -R some-symbol *
Run Code Online (Sandbox Code Playgroud)

但我需要该实用程序来自动检测以UTF-16(和朋友)编码的文件并适当地搜索它们.我的文件甚至还有字节排序标记FFEE,所以我甚至都没有寻找英雄的自动检测.

有什么建议?


我指的是Windows Vista和XP.

Pol*_*usB 6

一种解决方法是将您的 UTF-16 转换为 ASCII 或 ANSI

TYPE UTF-16.txt > ASCII.txt
Run Code Online (Sandbox Code Playgroud)

然后你可以使用 FINDSTR。

FINDSTR object ASCII.txt
Run Code Online (Sandbox Code Playgroud)


Mar*_*osi 1

根据 Damon Cortesi 的这篇博客文章,正如您发现的那样,grep 不适用于 UTF-16 文件。但是,它提出了这种解决方法:

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
Run Code Online (Sandbox Code Playgroud)

这显然是针对 Unix 的,不确定 Windows 上的等价物是什么。该文章的作者还提供了一个 shell 脚本来执行上述操作,您可以在 github 上找到该脚本

这仅greps UTF-16 文件。您还可以按正常方式 grep ASCII 文件。