[MacBook Pro] ダウンロードしたファイルが文字化けしていて困ったのでそれを直した方法
前書き
ダウンロードした zip ファイルを解答したらその中のファイルのいくつかが文字化けしていて困った。それを直した方法を書きます。
やり方
nkf というコマンドをインストールしました。nkf を使って文字化けを治すことができました。
まずは nkf をインストールします
ターミナルを立ち上げて brew でインストールしました。
$ brew install nkf Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> New Formulae boost-python3 chrome-export howdoi mdcat posh primer3 restview siril ==> Updated Formulae advancemame flow kubernetes-helm pandoc-citeproc akamai fltk libbitcoin pegtl amber fluent-bit libbitcoin-blockchain picard-tools angular-cli folly libbitcoin-consensus postgresql@9.5 apm-server fonttools libbitcoin-database pre-commit artifactory fossil libbitcoin-explorer presto auditbeat fribidi libbitcoin-node proguard aws-sdk-cpp gammaray libbitcoin-server py2cairo bandcamp-dl gdb liblwgeom py3cairo bartycrouch geeqie libphonenumber python bash gexiv2 librealsense qpdf bigloo gforth librsvg quicktype bit gifski libsass radare2 boost-python git-standup libspatialite rocksdb bzt git-town libzzip saldl cask gitless liquibase saltstack chuck global logstash selenium-server-standalone citus go macvim shadowsocks-libev closure-compiler go@1.8 mariadb@10.1 sip cockroach godep mbedtls snakemake coffeescript goenv metabase spigot conjure-up gpatch metricbeat sqlmap consul gradle mikutter stella cpprestsdk groonga miniupnpc syncthing cryfs grpc mlton taisei dbus gwyddion mockserver timewarrior dcmtk gxml mongoose tomcat dfmt hadolint mpd twarc dislocker haproxy mpdas txr docker heartbeat mpfr unibilium docker-completion heroku mrboom uriparser docker-compose hlint multimarkdown vala dtc htmldoc nailgun verilator duo_unix htop nasm vifm elasticsearch hugo neko vim elasticsearch@5.6 hwloc neovim vips embulk igv nginx wireguard-tools enchant influxdb node-build xpdf exempi jenkins octave xxhash expect joplin ortp youtube-dl fabio kaitai-struct-compiler osquery zanata-client fibjs kibana p7zip zimg filebeat kibana@5.6 packetbeat ==> Deleted Formulae go@1.6 go@1.7 ==> Downloading https://homebrew.bintray.com/bottles/nkf-2.1.4.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring nkf-2.1.4.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/nkf/2.1.4: 5 files, 343.4KB $ which nkf /usr/local/bin/nkf $
コマンド一発でインストールできました。
nkf の使い方
まずヘルプを見てみます。nkf –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.4 (2015-12-12)
Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).
Copyright (C) 1996-2015, The nkf Project.
$
文字化けファイルを修正する方法
まず文字化けファイルを確認します。
$ ls -tlr total 232 -rwxr-xr-x@ 1 nobi staff 300 Oct 28 15:54 ÉIÉäÉWÉiÉãéqÉeÅ[É}DžǬǢǃ.txt -rwxr-xr-x@ 1 nobi staff 50443 Oct 31 10:17 affinger4-child-yasasii2.zip -rwxr-xr-x@ 1 nobi staff 59462 Oct 31 10:17 affinger4-child-beast2.zip $ file *.txt ÉIÉäÉWÉiÉãéqÉeÅ[É}DžǬǢǃ.txt: Non-ISO extended-ASCII text, with CRLF line terminators $
これ ↓
ÉIÉäÉWÉiÉãéqÉeÅ[É}DžǬǢǃ.txt
が文字化けしちゃってるファイルです。.txt とついているのでテキストファイルなんですが、このままでは MacBook Pro では開くことはできません。
そこで nkf です。-g をつけて実行するとそのファイルの文字コードを判別してくれます。
$ nkf -g *.txt
Shift_JIS
$
Shift JIS と表示されていました。だから文字化けしてしまったんでしょう。これを UTF8 に直します。
$ nkf -w8 *.txt > output.txt $ cat output.txt /***無料特典子テーマについて***/ AFFINGER4専用のオリジナル子テーマです。 親テーマ(STINGER PRO)をインストールした後に有効化して下さい Yasasii2の使い方 http://stinger-manual.com/yasasii2/ Beast2の使い方 http://stinger-manual.com/beast2/ ※子テーマに関する保証及びサポートはございません$
- nkf -w8 の -w8 が UTF8 に変換しろという指示です
- *.txt が文字化けしちゃってるファイルです
- output.txt は文字化けを直したファイルを書き込んでます
これにより cat output.txt で開くことができるようになりました。output.txt は別のファイル名でももちろんオーケーです。
以上!