Getting "sed error - illegal byte sequence" (in bash)

BashSed

Bash Problem Overview


Doing some stream editing to change the nasty Parallels icon. It's poorly developed and embedded into the app itself rather than being an image file. So I've located this sed command that has some good feedback:

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

It returns sed: RE error: illegal byte sequence

Can anyone explain what this means? What part of the command is the problem?

Bash Solutions


Solution 1 - Bash

Try setting the LANG environment variable (LANG=C sed ...) or use one of the binary sed tools mentioned here: https://stackoverflow.com/questions/2604964/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.

Solution 2 - Bash

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

Solution 3 - Bash

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.

Solution 4 - Bash

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaltView Question on Stackoverflow
Solution 1 - BashldrgView Answer on Stackoverflow
Solution 2 - BashMalströmView Answer on Stackoverflow
Solution 3 - Bashrjpeter2View Answer on Stackoverflow
Solution 4 - BashAdam WaiteView Answer on Stackoverflow