Mass file encoding conversion on linux

2

Hello, I need to do the file encoding conversion to utf-8 since the application will migrate from server.

In this application I have files with several encodings in fact, some in utf-8, others in iso-8859-1 and still files in windows-1252.

What I need to do is to migrate all to utf8. I was able to find this bash that does it. The problem is that it adds a blank line after each line of the file, in case if I have a file with 200 line, it turns to 400 leaving the whole code messy and difficult to understand.

I would like to know what I can do to avoid this blank line that it adds.

Here is the code I have to do this conversion:

#!/bin/bash

DIRETORIO=$1
LISTA="/tmp/lista-conversor.txt"

if [ -d "${DIRETORIO}" ]; then

echo -e "\nGerando lista de arquivos em \"${DIRETORIO}\" a serem analisados:\n"
find ${DIRETORIO} -type f > ${LISTA}
#-exec $DIRETORIO {} \;

while read ARQUIVO;do

        ISO_YES=$(file $ARQUIVO|grep 8859|wc -l)
        if [ "$ISO_YES" -eq 1 ]; then
                echo "iso-8859 detectado em $ARQUIVO"
                iconv -f iso-8859-1 -t utf-8 ${ARQUIVO} >  ${ARQUIVO}.new && echo -e "Arquivo ${ARQUIVO} convertido com sucesso\n"
                cp $ARQUIVO ${ARQUIVO}.bkp
                mv ${ARQUIVO}.new ${ARQUIVO}
        fi

done < ${LISTA}

else
echo -e "Informe um diretorio\n\nEx:\n${0} <diretorio>"
fi
    
asked by anonymous 22.09.2017 / 14:22

1 answer

1

What about:

#!/bin/bash

DIR=$1

if [ ! -d "${DIR}" ]; then
    echo -e "Informe um diretorio\nEx:\n${0} <diretorio>"
    exit 1;
fi

for ARQ in $( find ${DIR} -name '*.txt' -type f );
do
    CONVERT=$( file ${ARQ} | grep "8859" | wc -l )

    if [ "$CONVERT" -eq 1 ]; then

            echo -n "Processando: '${ARQ}' ... "

            iconv -f iso-8859-1 -t utf-8 ${ARQ} -o ${ARQ}.tmp

            if [ $? -eq 0 ]; then
                echo "OK!"
            else
                echo "ERRO!"
            fi

            cp ${ARQ} ${ARQ}.bkp

            mv ${ARQ}.tmp ${ARQ}
    fi

done
    
25.09.2017 / 21:41