使用 BOM 搜索 UTF-8 文件的优雅方式? [英] Elegant way to search for UTF-8 files with BOM?

查看:29
本文介绍了使用 BOM 搜索 UTF-8 文件的优雅方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,我需要递归搜索所有以 UTF-8 字节顺序标记 (BOM) 开头的文件的目录.我目前的解决方案是一个简单的 shell 脚本:

For debugging purposes, I need to recursively search a directory for all files which start with a UTF-8 byte order mark (BOM). My current solution is a simple shell script:

find -type f |
while read file
do
    if [ "`head -c 3 -- "$file"`" == $'xefxbbxbf' ]
    then
        echo "found BOM in: $file"
    fi
done

或者,如果您更喜欢简短的、不可读的单行代码:

Or, if you prefer short, unreadable one-liners:

find -type f|while read file;do [ "`head -c3 -- "$file"`" == $'xefxbbxbf' ] && echo "found BOM in: $file";done

它不适用于包含换行符的文件名,但无论如何都不会出现此类文件.

It doesn't work with filenames that contain a line break, but such files are not to be expected anyway.

有没有更简洁或更优雅的解决方案?

Is there any shorter or more elegant solution?

有什么有趣的文本编辑器或文本编辑器的宏吗?

Are there any interesting text editors or macros for text editors?

推荐答案

这个简单的命令如何,它不仅可以找到而且可以清除讨厌的 BOM?:)

What about this one simple command which not just finds but clears the nasty BOM? :)

find . -type f -exec sed '1s/^xEFxBBxBF//' -i {} ;

我喜欢查找":)

警告 以上将修改包含这三个字符的二进制文件.

Warning The above will modify binary files which contain those three characters.

如果您只想显示 BOM 文件,请使用这个:

If you want just to show BOM files, use this one:

grep -rl $'xEFxBBxBF' .

这篇关于使用 BOM 搜索 UTF-8 文件的优雅方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆