テキストデータ変換用のコマンドを整理してみた。
echo "日本語" > sample.txt
nkf -jLu sample.txt > sample.ISO-2022-JP.LF.txt
nkf -jLw sample.txt > sample.ISO-2022-JP.CRLF.txt
nkf -e sample.txt > sample.EUC-JP.LF.txt
nkf -s sample.txt > sample.Shift_JIS.LF.txt
nkf -w sample.txt > sample.UTF-8.LF.txt
nkf -jLu sample.txt > sample.ISO-2022-JP.LF.txt
nkf -jLw sample.txt > sample.ISO-2022-JP.CRLF.txt
nkf -e sample.txt > sample.EUC-JP.LF.txt
nkf -s sample.txt > sample.Shift_JIS.LF.txt
nkf -w sample.txt > sample.UTF-8.LF.txt
iconvを使ってもいい。
iconv -f iso-2022-jp -t utf-8 < sample.ISO-2022-JP.LF.txt > sample.UTF-8.LF.txt
fileコマンドを使うと、対象ファイルの情報を表示することができる。
file sample.txt
file sample.ISO-2022-JP.CRLF.txt
file sample.ISO-2022-JP.CRLF.txt
nkfを使って改行コードも指定したい場合は次のようにする。ここでは、 w で utf-8、Lu で 改行コードをLFと指定している。
nkf -wLu sample.ISO-2022-JP.CRLF.txt > sample.UTF-8.LF.txt
次の例では、 w で utf-8、Lm で 改行コードをCRLFと指定している。
nkf -wLm sample.ISO-2022-JP.CRLF.txt > sample.UTF-8.CRLF.txt
改行コードは tr でも置換可能。dos2unixやunix2dosといったコマンドが使えることもある。
関連書籍: