Accents in Bash

0

When I generate a query from my database, it returns an information to me, which I save in a variable and then send it by email from the bash using "mail".

The problem I have is that, as some words have accents, the Bash does not recognize it and exploits me, ignoring all that row. Does anyone know how to avoid this?

I tried with:

OBSERVACIONES_ARREGLADO=$(echo "$OBSERVACIONES" | sed 'y/áÁàÀãÃâÂéÉêÊíÍóÓõÕôÔúÚñÑçǪº/aAaAaAaAeEeEiIoOoOoOuUnNcCao/')

But he died of laughter haha

    
asked by JuanManuel245 16.08.2018 в 17:05
source

1 answer

0

Something I have done is to make queries and export them in some way to a file, be it a csv, etc.

Since you have that csv you can pass it a $file <archivo que exportaste> and see the coding there.

Afterwards, that encoding is used in the command:

$iconv -f <la codificación que obtuviste> -t <a la que la quieres pasar> <archivo que exportaste> > archivo_nueva_codificacion.txt

Here there are good examples to a related question.

Just as an additional comment, I know you asked for Linux, but if at some point you are doing a web service that queries a database and has strange characters, you could generate a function similar to this in python that I use. Of course, according to the language of your preference, I just want to make you understand the idea.

def func_encode_to_utf(x):
    if 'utf-8' not in chardet.detect(x)['encoding'].lower():
        x = x.decode(chardet.detect(x)['encoding']).encode('utf-8')
    return x

And the mechanics are the same, know the coding of a character and then, if it is not "UTF-8" (in my case), I pass it to "UTF-8"

    
answered by 08.11.2018 в 15:40