2
Hello, I’m needing to do the encoding conversion of files to utf-8 since the application will migrate from server.
In this application I have files with several encodings actually, some in utf-8, others in iso-8859-1 and still files in windows-1252.
What I need to do is migrate everyone to utf8. I managed to find that bash that does this. 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 into 400 leaving the whole code messy and difficult to understand.
I wonder what I can do to avoid that blank line he adds.
That’s the code I have to make 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
Add the parameter in your script
-c
after the commandiconv
and see if it works. :iconv -c -f iso-8859-1 -t utf-8
[...]– gfleck
I will do it @gfleck, can you inform me what this command does?
– Joel Piccoli da Rosa
Right, this command is what effectively converts your files. The rest of the script is to automate the process, but that line is the heart of your script.
– gfleck
@Joelpiccolidarosa, I can’t figure out why to double the number of lines. You can show the
wc f
before and after conversion? and already now thefile -i f
fo file concerned?– JJoao