获取“ sed 错误-非法字节序列”(在 bash 中)

进行一些流编辑以更改讨厌的 Parallels 图标。它开发得很差,并且嵌入到应用程序中,而不是作为一个图像文件。所以我找到了这个 sed 命令,它有一些很好的反馈:

sudo sed -i.bak s/Parallels_Desktop_Overlay_128/Parallels_Desktop_Overlay_000/g /Applications/Parallels\ Desktop.app/Contents/MacOS/prl_client_app

它返回 sed: RE error: illegal byte sequence

有人能解释一下这是什么意思吗? 命令的哪一部分有问题?

67115 次浏览

Try setting the LANG environment variable (LANG=C sed ...) or use one of the binary sed tools mentioned here: binary sed replacement

Why the error?

Without LANG=C sed assumes that files are encoded in whatever encoding is specified in LANG and the file (being binary) may contain bytes which are not valid characters in LANG's encoding (thus you could get 'illegal byte sequence').

Why does LANG=C work?

C just happens to treat all ASCII characters as themselves and non-ASCII characters as literals.

LANG=C alone didn't do the trick for me but adding LC_CTYPE=C as well solved it.

I managed to do it by running:

unset LANG

before the sed command.

Not sure what I've done or why it works but it did.

In addition to LANG=C and LC_CTYPE=C, I had to do LC_ALL=C to get this to work.

LC_ALL overrides all individual LC_* categories. Thus, the most robust approach is to use LC_ALL=C sed ... - no need to also deal with the other variables.