Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert files between UTF-8 and ISO-8859 on Linux

Tags:

linux

iconv

Every time that I get confronted with Unicode, nothing works. I'm on Linux, and I got these files from Windows:

$file *
file1: UTF-8 Unicode text
file2: ISO-8859 text
file3: ISO-8859 text

Nothing was working until I found out that the files have different encodings. I want to make my life easy and have them all in the same format:

iconv -f UTF-8 -t ISO-8859 file1 > test
iconv: conversion to `ISO-8859' is not supported
Try `iconv --help' or `iconv --usage' for more information.

I tried to convert to ISO because that's only 1 conversion + when I open those ISO files in gedit, the German letter "ü" is displayed just fine. Okay, next try:

iconv -f ISO-8859 -t UTF-8 file2 > test
iconv: conversion from `ISO-8859' is not supported
Try `iconv --help' or `iconv --usage' for more information.

but obviously that didn't work.

like image 680
user3182532 Avatar asked Aug 02 '17 15:08

user3182532


2 Answers

ISO-8859-x (Latin-1) encoding only contains very limited characters, you should always try to encode to UTF-8 to make life easier.

And utf-8 (Unicode) is a superset of ISO 8859 so it will be not surprised you could not convert UTF-8 to ISO 8859

It seems command file just give a very limited info of the file encoding

You could try to guess the from encoding either ISO-8859-1 or ISO-8859-15 or the other from 2~14 as suggested in the comment by @hobbs

And you could get a supported encoding of iconv by iconv -l

If life treats you not easy with guessing the real file encoding, this silly script might help you out :D

like image 110
armnotstrong Avatar answered Sep 29 '22 00:09

armnotstrong


As in other answers, you can list out the supported formats

iconv -l | grep 8859 

A grep will save your time to find which version of your encoding is/are supported. You can provide the <number> as in my example or ISO or any expected string in your encoding.

like image 39
smilyface Avatar answered Sep 29 '22 00:09

smilyface