Search code examples
bashshelltype-conversion

having problem with converting in bash with Iconv


iconv: illegal input sequence at position 0

If I have a string written in Hebrew language but in English keyboard layout, I want the script to return the same string but in Hebrew keyboard layout.

I'm a new bash user, so it might be a silly problem but I really can't find the answer.

#!/bin/bash

read -p "Give me a word: " word

echo "$word" | iconv -t cp1255 | tr $(echo "[/'\קראטוןםפ\]\[שדגכעיחלךף,\זסבהנמצתץ./'\קראטוןםפ}{שדגכעיחלך:\"|זסבהנמצ><?@#$^&~\]" | iconv -t cp1255) "[qwertyuiop\[\]asdfghjkl;'\\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:\"|ZXCVBNM<>?@#$^&~\`]"

echo "$word" | tr "[qwertyuiop\[\]asdfghjkl;'\\zxcvbnm,./QWERTYUIOP{}ASDFGHJKL:\"|ZXCVBNM<>?@#$^&~\`]" $(echo "[/'\קראטוןםפ\]\[שדגכעיחלךף,\זסבהנמצתץ./'\קראטוןםפ}{שדגכעיחלך:\"|זסבהנמצ><?@#$^&~\]"| iconv -t cp1255) | iconv -t cp1255

Solution

  • As I understand your question, there is just a simple map of characters, where every character from one set should be replaced by a character from the second set. tr (sadly) does not support multi-byte characters nor locale.

    GNU sed support Unicode. With y command, you can translate between characters:

    $ echo 'קראטו' | LC_ALL=C.UTF-8 sed 'y/קראטו/qwert/'
    qwert