[파일인코딩] 변경하기

  • Object
Linux 에서 iconv를 사용해서 파일의 속성이 UTF-8 -> ANSI type의 파일 로 변경

  • Agenda
1. 파일 타입 확인
2. 파일 타입 변경하기
3. 지원하는 파일 타입 알아보기
4. 폴더안에 파일의 파일타입 모두 변경하기

1. 파일 타입 확인
$file a.txt
Little-endian UTF-16 Unicode English text, with CRLF line terminators


2. 파일 타입 변경하기
$iconv -c -f UTF-16 -t MSCP949 a.txt > a.ansi.txt
-c     When  this  option is given, characters that cannot be converted
       are silently discarded,  instead  of  leading  to  a  conversion
       error.
한글일때 MSCP949 가 아닌 EUC-KR 의 경우 한글이 사라지는 현상이 발생

3. 지원하는 파일 타입 알아보기 (list)
$iconv -l
..생략
UNICODE, UNICODEBIG, UNICODELITTLE, US-ASCII, US, UTF-7, UTF-8, UTF-16,  UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, UTF7, UTF8, UTF16, UTF16BE,  UTF16LE, UTF32, UTF32BE, UTF32LE, VISCII, WCHAR_T, WIN-SAMI-2, WINBALTRIM,  WINDOWS-31J, WINDOWS-874, WINDOWS-936, WINDOWS-1250, WINDOWS-1251,  WINDOWS-1252, WINDOWS-1253, WINDOWS-1254, WINDOWS-1255, WINDOWS-1256,  WINDOWS-1257, WINDOWS-1258, WINSAMI2, WS2,

4. 폴더안에 파일의 파일타입 모두 변경하기 (changeCharType.sh)
$./c.sh UTF-16 MSCP949 /data/source/ /data/target/
#!/bin/bash
#to UTF-8, from EUC-KR
#sdir=/source/dms/
#tdir=/target/dms/

if [ $# -ne 4 ]; then
exit
fi;

from=$1
to=$2
source=$3
target=$4

# dms폴더 밑에 001, 002, 003 폴더가 있다고 가정.
names=( 001 002 003  )

for name in ${names[@]}
do
        tdir=$target/$name
        if [ ! -d $tdir ]; then
                mkdir -p $tdir
        fi

        sdir=$source$name
        cd $sdir

        for f in $( ls . ); do
                if [ -f $f ]; then
                echo $sdir/$f
                iconv -c -f $from -t $to $sdir/$f > $tdir/$f
        fi
done

done

기타.
/usr/lib/gconv/gconv-modules 를 통해서 alias를 살펴볼 수 있다.
UHC의 경우 완성형 한글Character Set 을 의미한다.

Referred Links

댓글

가장 많이 본 글