Kodama's home / tips.

DOS と UNIX のテキスト変換

漢字コ−ドと 行末コ−ドを変換する必要がある. nkf のoption で全てかたがつくようになったので nkf だけでやるのが良いかも. nkf -de とか nkf -cs を使おう. fromdos とか todos も有る.

以下の スクリプトを unix2dos とかの名で作っておいて, ln -s unix2dos dos2unix とかで link を作っておく. 使う前に よ-く読んでね. ^M は ^とM ではなく コントロ−ルコ−ドだよ. sed の代わりに tr, gawk, prel とか ruby でも やってみてね. フィルタでなく ファイルを書き換えるようになってるのはあまり良くないなぁ. 便利そうに思ったんだけど....

#!/bin/sh 
# dos2unix , unix2dos
# convert DOS/sjis text to UNIX/EUC
# or UNIX/EUC to DOS/sjis

PROG=`basename $0`
if [ "$PROG" = "dos2unix" ] ; then 
	# DOS/sjis のテキストを UNIX/euc に変換.
	tmpFile="/tmp/`basename $0`.$$"
	for i ; do
		(nkf -S -e $i | sed -e 's/^M$//' > $tmpFile) && mv $tmpFile $i
	done
	exit 0
fi

if [ "$PROG" = "unix2dos" ];then 
	# UNIX/euc のテキストを DOS/sjis に変換.
	tmpFile="/tmp/`basename $0`.$$"
	for i ; do
		(nkf -s $i | sed -e 's/^M$//' -e 's/$/^M/' > $tmpFile) && mv $tmpFile $i
	done
	exit 0
fi
exit 1

Kodama's home / tips.