NKFによるURL Decode

nkf convert character

URLをリダイレクトしたりする場合に、日本語文字や特殊文字が含まれている場合にブラウザーでは理解出来ても、プログラムの方で処理出来ない場合があります。そんな特に、URLをEncodeしてリダイレクト先に設定したりしますが、EncodeしてあるURLをサクット確認したい場合にNKFを利用して確認する事も可能です。

In case of decode an encoded URL, you can use NFK command to decode it.

~ [13:44:31]> 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=<encoding>        Specify the input encoding
 --oc=<encoding>        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.
~ [13:44:35]>
NKF
nkf
~ [13:50:07]> echo "https://variable.jp/2021/09/26/rdbms%e3%81%ab%e3%81%8a%e3%81%91%e3%82%8b%e7%a9%ba%e9%96%93%e6%83%85%e5%a0%b1/" | nkf --url-input
https://variable.jp/2021/09/26/rdbmsにおける空間情報/
~ [13:51:03]>

上記の様にURLをDecodeする事が可能です、また以下の様にWindowsとLinux等でファイルをやり取りしたりする場合に文字コードが合わず読めない場合も変換する事が可能です。

文字コードの確認とShift_JISから、UTF-8への変換する場合

~/win [14:00:39]> cat windows_file.txt
���A������肢�������܂��B
~/win [14:00:43]> file windows_file.txt
windows_file.txt: Non-ISO extended-ASCII text, with CRLF line terminators
~/win [14:00:52]> nkf -g windows_file.txt
Shift_JIS
~/win [14:01:00]> nkf -w windows_file.txt
ご連絡をお願いいたします。
~/win [14:01:09]> nkf -w windows_file.txt > windows_file_utf8.txt
~/win [14:01:29]> nkf -g windows_file_utf8.txt
UTF-8
~/win [14:01:42]> cat windows_file.txt
���A������肢�������܂��B
~/win [14:01:45]> cat windows_file_utf8.txt
ご連絡をお願いいたします。
~/win [14:01:51]>

UTF-8のファイルをWindows用にShift-JISに変換する場合

~/win [16:10:28]> nkf -g costs2022-3.csv
UTF-8
~/win [16:13:22]> nkf -s costs2022-3.csv > convert_fees.csv
~/win [16:13:25]> nkf -g convert_fees.csv
Shift_JIS
~/win [16:13:32]>

NKFは幅広く利用されているので、aptやyum等でインストール可能です。

root@ubuntu:/mnt/c/Linux# apt install nkf
Reading package lists... Done
Building dependency tree
Reading state information... Done
nkf is already the newest version (1:2.1.4-1ubuntu2).
The following packages were automatically installed and are no longer required:
  gir1.2-harfbuzz-0.0 icu-devtools libdumbnet1 libglib2.0-bin libglib2.0-dev libglib2.0-dev-bin libgraphite2-dev libharfbuzz-dev libharfbuzz-gobject0 libharfbuzz-icu0 libicu-dev libicu-le-hb-dev
  libicu-le-hb0 libiculx60 libllvm8 libpcre16-3 libpcre3-dev libpcre32-3 libpcrecpp0v5 libsqlite3-dev libxml2-dev libxslt1-dev pkg-config python3-distutils python3-lib2to3 ruby-build
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 46 not upgraded.
root@ubuntu:/mnt/c/Linux#

For CentOS

https://centos.pkgs.org/7/epel-x86_64/nkf-2.1.3-5.el7.x86_64.rpm.html

For Windows

http://www.vector.co.jp/soft/win95/util/se295331.html

カテゴリー:

最近のコメント

表示できるコメントはありません。