UTF8 encoding and GB2312 encoding are different. In sqlplus, importing UTF8 encoded sql script will cause garbled error. At this time, UTF8 encoding needs to be converted into GB2312 encoding, but the conversion is very troublesome. Edit teaches you how to convert UTF8 encoding into GB2312 encoding in Linux. When
Background
I bulk import sql script UTF8-encoded using the oracle sqlplus, do not know how to set due recognition to make sqlplus UTF8 format, resulting in garbled, errors such as the wrong line And so that the work can not continue, in the case of google no fruit, I had to find a way to convert the code.
Due to the large number of files, manual conversion is too cumbersome, so I thought of batch conversion with scripts. Fortunately, there are many related scripts on the Internet. The only trouble to implement is the UTF8 BOM markup.
Content:
The code is as follows:
#! /bin/bash
for loop in `find . -type f -name “*.sql” -print`do
echo $loop
mv -f $loop $loop.tmp
dos2unix $loop.tmp
file_check_utf8=‘file_check_utf8.log’
sed -n ‘1l’ $loop.tmp 》$file_check_utf810. if grep ‘^\\\\357\\\\273\\\\277’ $file_check_utf8 》/dev/null 2》&111. then
echo ‘UTF-8 BOM’
sed -n -e ‘1s/^. . //’ -e ‘w intermediate.txt’ $loop.tmp14. iconv -f UTF-8 -t GB2312 -o $loop intermediate.txt15. rm -rf intermediate.txt
rm -rf $loop.tmp
elif iconv -f UTF-8 -t GB2312 $loop.tmp 》/dev/null 2》&118. then
echo ‘UTF-8’< Br>
iconv -f UTF-8 -t GB2312 -o $loop $loop.tmp21. rm -rf $loop.tmp
else
echo ‘ANSI’
mv -f $loop.tmp $loop
fi
rm -rf $file_check_utf8
#simulate unix2dos, requiring the last line of the text file to have a newline 28. sed -n -e ‘s/$/\
/g’ -e ‘w ’$loop.tmp $loop29. mv -f $loop.tmp $loop
done
#! /bin/bash
for loop in `find . -type f -name “*.sql” -print`do
echo $loop
mv -f $loop $loop.tmp
dos2unix $loop.tmp
file_check_utf8=‘file_check_utf8.log’
sed -n ‘1l’ $loop.tmp 》$file_check_utf810. if grep ‘^\\\\357\\\\273\\\\277’ $file_check_utf8 》/dev/null 2》&111. then
echo ‘UTF-8 BOM’
sed -n -e ‘1s/^. . //’ -e ‘w intermediate.txt’ $loop.tmp14. iconv -f UTF-8 -t GB2312 -o $loop intermediate.txt15. rm -rf intermediate.txt
rm -rf $loop.tmp
elif iconv -f UTF-8 -t GB2312 $loop.tmp 》/dev/null 2》&118. then
echo ‘UTF-8’< Br>
iconv -f UTF-8 -t GB2312 -o $loop $loop.tmp21. rm -rf $loop.tmp
else
echo ‘ANSI’
mv -f $loop.tmp $loop
fi
rm -rf $file_check_utf8
#simulate unix2dos, requiring the last line of the text file to have a newline 28. sed -n -e ‘s/$/\
/g’ -e ‘w ’$loop.tmp $loop29. mv -f $loop.tmp $loop
done
Explanation
1. To deal with the UTF8 BOM, I have not found a good way, and finally judged with sed+grep, if the first three bytes are \\\\357\\\\273\\\\277, then The file must be UTF8, use sed to remove the three bytes and convert
2. To avoid duplication or omission, use iconv in the script. The file without the BOM tries to convert one. The conversion success indicates that the file is UTF8, otherwise the description is ANSI or GB2312
3. Regarding the last sed command, it is because there is no unix2dos command on my system, so The simulation was carried out in order to facilitate viewing and editing under Windows.
The above is the introduction of UTF8 encoding batch conversion to GB2312 encoding under Linux. After conversion, it can solve garbled problems, etc. Mirror batch conversion, have you learned?
CKEditor is commonly referred to fCKEDITOR, it is an excellent web editor, can supp
Using the xargs command on a Linux system can solve a lot of problems, especially t
Win8 system has Game Manager? You may not have noticed it, it really does. However, it is really dif
The dd command is very useful on Linux systems, Linux commands that can copy files.
CentOS 7 tips for resetting the root password
How to open and use Win8 Game Manager
Linux system NBMA and BMA exchange method introduction
Steps to set up UFW firewall on Ubuntu
Linux system hard disk failure repair method
How to identify open and fopen functions in Linux
Ubuntu OpenSC installation method
What should I do if Xmanager fails to connect after the Solaris system is installed?
What should I do if libpng fails to compile after Gentoo upgrade?
Linux prompts when you use Mkdir can not create directory ‘test’?
Win7 closes the folder sharing method
How to enable IPv6 protocol in Windows Server 2003
How to export themes in Windows 8 system
Win8 uses its own decompression tool
How does win10 set the edge as the default browser? Edge set to default browser tutorial
Windows 8 file drag and drop, custom lock screen function
Windows 7 Fast Sharing Internet Wireless Network Connection
Win8 touch version IE10 will enable Flash by default
Win10 RS2 updated what? Win10 RS2 final version number 1704 first exposure