macにnkfコマンドがないだと!?じゃあ入れよう!

apple-mac-viruses

nkfコマンドって何よ?

端的には『文字コード変換ツール』だ。 今回やりたかったのは改行コードの変更だったのでtrコマンドでよかったのだけど、nkfコマンドの素敵具合を思い出して、過去のshell資産などを使えるかなぁと実行しようとしたら、

bash: ./hogehoge.sh: /bin/sh^M: bad interpreter: No such file or directory

こんなエラーが出て悲しくなった。 悲しくなったので楽しいことをしようと考えた。 ただそれだけだ。 尚、macにtrコマンドは入ってる。


じゃあインストールだ!

わたくしは何を血迷ったかmacportでパッケージ管理してるので、

$ port search nkf
nkf @2.1.3_3 (textproc, japanese)
    Network Kanji code conversion Filter

p5-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

p5.8-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

p5.10-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

p5.12-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

p5.14-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

p5.16-nkf @2.130.0 (perl, textproc, japanese)
    A perl extension module to use nkf.

Found 7 ports.
$ sudo port install nkf
--->  Fetching archive for nkf
--->  Attempting to fetch nkf-2.1.3_3.darwin_13.x86_64.tbz2 from http://jog.id.packages.macports.org/macports/packages/nkf
--->  Attempting to fetch nkf-2.1.3_3.darwin_13.x86_64.tbz2.rmd160 from http://jog.id.packages.macports.org/macports/packages/nkf
--->  Installing nkf @2.1.3_3
--->  Activating nkf @2.1.3_3
--->  Cleaning nkf
--->  Updating database of binaries: 100.0%
--->  Scanning binaries for linking errors: 100.0%
--->  No broken files found.

でOK

Homebrewの方は、 [Mac] nkf を Homebrew でインストール ここらを参照。


nkfコマンドの使い方

ついでなので、自分健忘録にコマンドの使い方をまとめておく。

まずはhelpを見る。 UNIXやLinux、macを触る際の基本だね。 大抵オプションは-h--helpだ。

$ nkf --help
Usage:  nkf -[flags] [--] [in file] .. [out file for -O flag]
 j/s/e/w  Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP
          UTF options is -w[8[0],{16,32}[{B,L}[0]]]
 J/S/E/W  Specify input encoding ISO-2022-JP, Shift_JIS, EUC-JP
          UTF option is -W[8,[16,32][B,L]]
 m[BQSN0] MIME decode [B:base64,Q:quoted,S:strict,N:nonstrict,0:no decode]
 M[BQ]    MIME encode [B:base64 Q:quoted]
 f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl
 Z[0-4]   Default/0: Convert JISX0208 Alphabet to ASCII
          1: Kankaku to one space  2: to two spaces  3: HTML Entity
          4: JISX0208 Katakana to JISX0201 Katakana
 X,x      Convert Halfwidth Katakana to Fullwidth or preserve it
 O        Output to File (DEFAULT 'nkf.out')
 L[uwm]   Line mode u:LF w:CRLF m:CR (DEFAULT noconversion)
 --ic=        Specify the input encoding
 --oc=        Specify the output encoding
 --hiragana --katakana  Hiragana/Katakana Conversion
 --katakana-hiragana    Converts each other
 --{cap, url}-input     Convert hex after ':' or '%'
 --numchar-input        Convert Unicode Character Reference
 --fb-{skip, html, xml, perl, java, subchar}
                        Specify unassigned character's replacement
 --in-place[=SUF]       Overwrite original files
 --overwrite[=SUF]      Preserve timestamp of original files
 -g --guess             Guess the input code
 -v --version           Print the version
 --help/-V              Print this help / configuration
Network Kanji Filter Version 2.1.3 (2013-11-22) 
Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).
Copyright (C) 1996-2013, The nkf Project.

文字コードチェック

テキストやスクリプトの文字コードを調べたい場合-gでOK

$ nkf -g hoge.txt
ASCII

いやこの例だと意味ないじゃん。w 日本語の文字コードを調べる場合に使うので、UTF-8とかShift_JISとかEUC-JPという返り値が返ってくる。 昔からの癖でスクリプト類に2バイト文字使うの嫌いなんだよな……。

文字コード変換

とりあえず、Shift_JIS変換、EUC変換、JIS変換、UTF-8変換を記載しておく。 UTF-16は今のところ使ったことがないので省略。

SJISコードへ変換

Shift_JIS変換は、-s 上が元テキストが残る形、下は上書き。

$ nkf -s hoge.txt > moga.txt
$ nkf -s --overwrite hoge.txt

EUCコードへ変換

EUC変換は、-e 上が元テキストが残る形、下は上書き。

$ nkf -e hoge.txt > moga.txt
$ nkf -e --overwrite hoge.txt

JISコードへ変換

JIS変換は、-j 上が元テキストが残る形、下は上書き。

$ nkf -j hoge.txt > moga.txt
$ nkf -j --overwrite hoge.txt

UTF-8(BOMなし)コードへ変換

UTF-8(BOMなし)変換は、-w 最近、Java周りでxmlファイルにBOMがついてることがあり、その場合エラーが発生するので、この変換で取り去るといいだろう。 上が元テキストが残る形、下は上書き。

$ nkf -w hoge.txt > moga.txt
$ nkf -w --overwrite hoge.txt

UTF-8(BOMあり)コードへ変換

UTF-8(BOMあり)変換は、-w8 上が元テキストが残る形、下は上書き。

$ nkf -w8 hoge.txt > moga.txt
$ nkf -w8 --overwrite hoge.txt

改行コード変換

改行コードはWindowsがCR+LF、Unix/Linux/macがLFとなる。ちなみに昔のmacはCRで変換の際はWindowsと同様と考えてもらってかまわない。

Windows > Unix/Linux/mac

上が元テキストが残る形、下は上書き。個人的には元テキストを残した形で後で消すなりなんなりした方がいいと思うが、事前に元テキストのコピーを変換したい場合は--overwriteでもよい。

$ nkf -Lu win.txt > mac.txt
$ nkf -Lu --overwrite hoge.txt 

Unix/Linux/mac > Windows

上が元テキストが残る形、下は上書き。

$ nkf -Lw mac.txt > win.txt
$ nkf -Lw --overwrite hoge.txt

改行変換はtrコマンドでも出来る。

Windows > Unix/Linux/mac

$ tr -d \\r  mac.txt

改行変換は、nkfでもtrでもsedでもawkでもperlでもrubyでもpythonでもphpでも出来る。 vimとかemacsでも。

改行コード変換については、ここが詳しかった。 ちょいと古いけど。

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>