ChatGPT解决这个技术问题 Extra ChatGPT

RE error: illegal byte sequence on Mac OS X

I'm trying to replace a string in a Makefile on Mac OS X for cross-compiling to iOS. The string has embedded double quotes. The command is:

sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure

And the error is:

sed: RE error: illegal byte sequence

I've tried escaping the double quotes, commas, dashes, and colons with no joy. For example:

sed -i "" 's|\"iphoneos-cross\"\,\"llvm-gcc\:\-O3|\"iphoneos-cross\"\,\"clang\:\-Os|g' Configure

I'm having a heck of a time debugging the issue. Does anyone know how to get sed to print the position of the illegal byte sequence? Or does anyone know what the illegal byte sequence is?

Illegal byte sequence sounds like something you get when feeding 8-bit ascii to something that expects utf-8.
Can you try: LC_CTYPE=C && LANG=C && sed command
Thanks folks. Its was the LANG thing. Sigh....
@user2719058: BSD sed (as also used on OS X) requires -i '' (separate, empty-string option-argument) for in-place updating without a backup file; with GNU sed, only -i by itself works - see stackoverflow.com/a/40777793/45375
Plus one for the LANG thing. Good grief, that's obscure, non-obvious and surprisingly difficult to research.

m
mklement0

A sample command that exhibits the symptom: sed 's/./@/' <<<$'\xfc' fails, because byte 0xfc is not a valid UTF-8 char.
Note that, by contrast, GNU sed (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an error.

Using the formerly accepted answer is an option if you don't mind losing support for your true locale (if you're on a US system and you never need to deal with foreign characters, that may be fine.)

However, the same effect can be had ad-hoc for a single command only:

LC_ALL=C sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure

Note: What matters is an effective LC_CTYPE setting of C, so LC_CTYPE=C sed ... would normally also work, but if LC_ALL happens to be set (to something other than C), it will override individual LC_*-category variables such as LC_CTYPE. Thus, the most robust approach is to set LC_ALL.

However, (effectively) setting LC_CTYPE to C treats strings as if each byte were its own character (no interpretation based on encoding rules is performed), with no regard for the - multibyte-on-demand - UTF-8 encoding that OS X employs by default, where foreign characters have multibyte encodings.

In a nutshell: setting LC_CTYPE to C causes the shell and utilities to only recognize basic English letters as letters (the ones in the 7-bit ASCII range), so that foreign chars. will not be treated as letters, causing, for instance, upper-/lowercase conversions to fail.

Again, this may be fine if you needn't match multibyte-encoded characters such as é, and simply want to pass such characters through.

If this is insufficient and/or you want to understand the cause of the original error (including determining what input bytes caused the problem) and perform encoding conversions on demand, read on below.

The problem is that the input file's encoding does not match the shell's.
More specifically, the input file contains characters encoded in a way that is not valid in UTF-8 (as @Klas Lindbäck stated in a comment) - that's what the sed error message is trying to say by invalid byte sequence.

Most likely, your input file uses a single-byte 8-bit encoding such as ISO-8859-1, frequently used to encode "Western European" languages.

Example:

The accented letter à has Unicode codepoint 0xE0 (224) - the same as in ISO-8859-1. However, due to the nature of UTF-8 encoding, this single codepoint is represented as 2 bytes - 0xC3 0xA0, whereas trying to pass the single byte 0xE0 is invalid under UTF-8.

Here's a demonstration of the problem using the string voilà encoded as ISO-8859-1, with the à represented as one byte (via an ANSI-C-quoted bash string ($'...') that uses \x{e0} to create the byte):

Note that the sed command is effectively a no-op that simply passes the input through, but we need it to provoke the error:

  # -> 'illegal byte sequence': byte 0xE0 is not a valid char.
sed 's/.*/&/' <<<$'voil\x{e0}'

To simply ignore the problem, the above LCTYPE=C approach can be used:

  # No error, bytes are passed through ('á' will render as '?', though).
LC_CTYPE=C sed 's/.*/&/' <<<$'voil\x{e0}'

If you want to determine which parts of the input cause the problem, try the following:

  # Convert bytes in the 8-bit range (high bit set) to hex. representation.
  # -> 'voil\x{e0}'
iconv -f ASCII --byte-subst='\x{%02x}' <<<$'voil\x{e0}'

The output will show you all bytes that have the high bit set (bytes that exceed the 7-bit ASCII range) in hexadecimal form. (Note, however, that that also includes correctly encoded UTF-8 multibyte sequences - a more sophisticated approach would be needed to specifically identify invalid-in-UTF-8 bytes.)

Performing encoding conversions on demand:

Standard utility iconv can be used to convert to (-t) and/or from (-f) encodings; iconv -l lists all supported ones.

Examples:

Convert FROM ISO-8859-1 to the encoding in effect in the shell (based on LC_CTYPE, which is UTF-8-based by default), building on the above example:

  # Converts to UTF-8; output renders correctly as 'voilà'
sed 's/.*/&/' <<<"$(iconv -f ISO-8859-1 <<<$'voil\x{e0}')"

Note that this conversion allows you to properly match foreign characters:

  # Correctly matches 'à' and replaces it with 'ü': -> 'voilü'
sed 's/à/ü/' <<<"$(iconv -f ISO-8859-1 <<<$'voil\x{e0}')"

To convert the input BACK to ISO-8859-1 after processing, simply pipe the result to another iconv command:

sed 's/à/ü/' <<<"$(iconv -f ISO-8859-1 <<<$'voil\x{e0}')" | iconv -t ISO-8859-1

I'd say this is a much better option. First, I wouldn't want to lose multi-language support in all of Terminal. Second, the accepted answer feels like a global solution to a local problem - something to be avoided.
I had a couple of small tweaks to this. I'd appreciate feedback. stackoverflow.com/a/35046218/9636
LC_CTYPE=C sed 's/.*/&/' <<<$'voil\x{e0}' prints sed: RE error: illegal byte sequence for me on Sierra. echo $LC_ALL outputs en_US.UTF-8 FWIW.
@ahcox: Yes, because setting LC_ALL overrides all other LC_* variables, including LC_CTYPE, as explained in the answer.
@mklement0 Cool, this works: "LC_ALL=C sed 's/.*/&/' <<<$'voil\x{e0}'". Precedence explained here for my fellow inattentive ignoramuses: pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html
b
binarytemple_picsolve

Add the following lines to your ~/.bash_profile or ~/.zshrc file(s).

export LC_CTYPE=C 
export LANG=C

it actually works, but could you please explain why?
@HoangPham: Setting LC_CTYPE to C causes each byte in strings to be its own character without applying any encoding rules. Since a violation of (UTF-8) encoding rules caused the original problem, this makes the problem go away. However, the price you pay is that the shell and utilities then only recognize the basic English letters (the ones in the 7-bit ASCII range) as letters. See my answer for more.
Setting this permanently in your shell's startup files will disable many useful behaviors. You want to put this in only for individual commands which absolutely require it.
Too dangerous can may cause unexpected consequences. One could use LC_CTYPE=C sed …, i.e. only on the sed command.
This will completely disable support for Unicode characters in your shell. Goodbye emojis, fancy line drawing characters, letters with accents, .... Much better to just set this for the sed command only, as described in other answers.
V
Vitaly Zdanevich

My workaround had been using Perl:

find . -type f -print0 | xargs -0 perl -pi -e 's/was/now/g'

This one works great. And i've had no errors escaping special characters unlike the others. The previous ones gave me issues like "sed: RE error: illegal byte sequence" or sed: 1: "path_to_file": invalid command code .
Simple and no need for configurations etc. Love it.
D
Denis from Val Thorens

You simply have to pipe an iconv command before the sed command. Ex with file.txt input :

iconv -f ISO-8859-1 -t UTF8-MAC file.txt | sed 's/something/àéèêçùû/g' | .....

-f option is the 'from' codeset and -t option is the 'to' codeset conversion.

Take care of case, web pages usually show lowercase like that < charset=iso-8859-1"/> and iconv uses uppercase. You have list of iconv supported codesets in you system with command iconv -l

UTF8-MAC is modern OS Mac codeset for conversion.


Also see iconv and charset names on the iconv mailing list.
C
Community

mklement0's answer is great, but I have some small tweaks.

It seems like a good idea to explicitly specify bash's encoding when using iconv. Also, we should prepend a byte-order mark (even though the unicode standard doesn't recommend it) because there can be legitimate confusions between UTF-8 and ASCII without a byte-order mark. Unfortunately, iconv doesn't prepend a byte-order mark when you explicitly specify an endianness (UTF-16BE or UTF-16LE), so we need to use UTF-16, which uses platform-specific endianness, and then use file --mime-encoding to discover the true endianness iconv used.

(I uppercase all my encodings because when you list all of iconv's supported encodings with iconv -l they are all uppercase.)

# Find out MY_FILE's encoding
# We'll convert back to this at the end
FILE_ENCODING="$( file --brief --mime-encoding MY_FILE )"
# Find out bash's encoding, with which we should encode
# MY_FILE so sed doesn't fail with 
# sed: RE error: illegal byte sequence
BASH_ENCODING="$( locale charmap | tr [:lower:] [:upper:] )"
# Convert to UTF-16 (unknown endianness) so iconv ensures
# we have a byte-order mark
iconv -f "$FILE_ENCODING" -t UTF-16 MY_FILE > MY_FILE.utf16_encoding
# Whether we're using UTF-16BE or UTF-16LE
UTF16_ENCODING="$( file --brief --mime-encoding MY_FILE.utf16_encoding )"
# Now we can use MY_FILE.bash_encoding with sed
iconv -f "$UTF16_ENCODING" -t "$BASH_ENCODING" MY_FILE.utf16_encoding > MY_FILE.bash_encoding
# sed!
sed 's/.*/&/' MY_FILE.bash_encoding > MY_FILE_SEDDED.bash_encoding
# now convert MY_FILE_SEDDED.bash_encoding back to its original encoding
iconv -f "$BASH_ENCODING" -t "$FILE_ENCODING" MY_FILE_SEDDED.bash_encoding > MY_FILE_SEDDED
# Now MY_FILE_SEDDED has been processed by sed, and is in the same encoding as MY_FILE

++ for helpful techniques, especially file -b --mime-encoding for discovering and reporting a file's encoding. There are some aspects worth addressing, however, which I'll do in separate comments.
I think it's safe to say that the Unix world has embraced UTF-8 at this point: the default LC_CTYPE value is usually <lang_region>.UTF-8, so any file without a BOM (byte-order mark) is therefore interpreted as a UTF-8 file. It is only in the Windows world that the pseudo-BOM 0xef 0xbb 0xff is used; by definition, UTF-8 does not need a BOM and is not recommended (as you state); outside the Windows world, this pseudo-BOM causes things to break.
Re Unfortunately, iconv doesn't prepend a byte-order mark when you explicitly specify an endianness (UTF-16BE or UTF-16LE): that is by design: if you specify the endianness explicitly, there's no need to also reflect it via a BOM, so none is added.
Re LC_* / LANG variables: bash, ksh, and zsh (possibly others, but not dash) do respect the character encoding; verify in POSIX-like shells with an UTF-8-based locale with v='ä'; echo "${#v}": a UTF-8 aware shell should report 1; i.e., it should recognize the multi-byte sequence ä (0xc3 0xa4), as a single character. Perhaps even more importantly, however: the standard utilities (sed, awk, cut, ...) also need to be locale/encoding-aware, and while most of them on modern Unix-like platforms are, there are exceptions, such as awk on OSX, and cut on Linux.
It's commendable that file recognizes the UTF-8 pseudo-BOM, but the problem is that most Unix utilities that process file do not, and usually break or at least misbehave when faced with one. Without a BOM, file correctly identifies an all-7-bit bytes file as ASCII, and one that has valid UTF-8 multi-byte characters as UTF-8. The beauty of UTF-8 is that it is a superset of ASCII: any valid ASCII file is by definition a valid UTF-8 file (but not vice versa); it's perfectly to safe to treat an ASCII file as UTF-8 (which it technically is, it just happens to contain no multi-byte chars.)
l
lu_zero

My workaround had been using gnu sed. Worked fine for my purposes.


Indeed, GNU sed is an option if you want to ignore invalid bytes in the input stream (no need for the LC_ALL=C sed ... workaround), because GNU sed simply passes invalid bytes through instead of reporting an error, but note that if you want to properly recognize and process all characters in the input string, there is no way around changing the input's encoding first (typically, with iconv).
M
Magic Thighs

Does anyone know how to get sed to print the position of the illegal byte sequence? Or does anyone know what the illegal byte sequence is?

$ uname -a
Darwin Adams-iMac 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

I got part of the way to answering the above just by using tr.

I have a .csv file that is a credit card statement and I am trying to import it into Gnucash. I am based in Switzerland so I have to deal with words like Zürich. Suspecting Gnucash does not like " " in numeric fields, I decide to simply replace all

; ;

with

;;

Here goes:

$ head -3 Auswertungen.csv | tail -1 | sed -e 's/; ;/;;/g'
sed: RE error: illegal byte sequence

I used od to shed some light: Note the 374 halfway down this od -c output

$ head -3 Auswertungen.csv | tail -1 | od -c
0000000    1   6   8   7       9   6   1   9       7   1   2   2   ;   5
0000020    4   6   8       8   7   X   X       X   X   X   X       2   6
0000040    6   0   ;   M   Y       N   A   M   E       I   S   X   ;   1
0000060    4   .   0   2   .   2   0   1   9   ;   9   5   5   2       -
0000100        M   i   t   a   r   b   e   i   t   e   r   r   e   s   t
0000120                Z 374   r   i   c   h                            
0000140    C   H   E   ;   R   e   s   t   a   u   r   a   n   t   s   ,
0000160        B   a   r   s   ;   6   .   2   0   ;   C   H   F   ;    
0000200    ;   C   H   F   ;   6   .   2   0   ;       ;   1   5   .   0
0000220    2   .   2   0   1   9  \n                                    
0000227

Then I thought I might try to persuade tr to substitute 374 for whatever the correct byte code is. So first I tried something simple, which didn't work, but had the side effect of showing me where the troublesome byte was:

$ head -3 Auswertungen.csv | tail -1 | tr . .  ; echo
tr: Illegal byte sequence
1687 9619 7122;5468 87XX XXXX 2660;MY NAME ISX;14.02.2019;9552 - Mitarbeiterrest   Z

You can see tr bails at the 374 character.

Using perl seems to avoid this problem

$ head -3 Auswertungen.csv | tail -1 | perl -pne 's/; ;/;;/g'
1687 9619 7122;5468 87XX XXXX 2660;ADAM NEALIS;14.02.2019;9552 - Mitarbeiterrest   Z?rich       CHE;Restaurants, Bars;6.20;CHF;;CHF;6.20;;15.02.2019