Search code examples
linuxbashshellcharacter-encodingiconv

How to avoid iconv error to halt my program


I have a utf-8 document to be convert to big5 encoding using iconv with the code below

iconv -f utf-8 -t big5 $inputFile -o $outputFile

However there are some utf-8 characters encoding is not complete because I set byte size limit in each line in the document like 40 bytes in a line so some utf-8 characters will be cut.

Since the incomplete encoding of utf-8 characters leads to the error that iconv cannot find the corresponding big5 encode for the incomplete utf-8 characters encoding and the iconv stops.

Is there any why to avoid the iconv to halt and skip the incomplete utf-8 characters encoding and continue convert the following document to big5 encoding document?


Solution

  • I'm not sure that is what you are looking for, but, to quote man iconv:

    DESCRIPTION The iconv program converts the encoding of characters in inputfile, or from the standard input if no filename is specified, from one coded character set to another.


    OPTIONS

    -c Omit invalid characters from output.
    [...]

    The man is not really clear, but when you use that option, characters in the source file invalid given the source encoding are discarded.