NKF Command Line Tool
NKF: Character Encoding Conversion and Manipulation
The NKF (Network Kanji Filter) command is a powerful utility for handling character encodings on Unix-like systems. It allows you to convert text files between various Japanese character sets such as UTF-8, EUC-JP, Shift-JIS, and ISO-2022-JP. Beyond simple encoding conversion, NKF can also manipulate newline characters and perform MIME encoding and decoding, making it an indispensable tool for developers and system administrators working with text data.
NKF Usage Examples
Here are some common use cases for the NKF command:
Checking Character Encoding
To determine the character encoding of a file, use the -g option:
# To check the file's character code:
nkf -g <file>
Converting to UTF-8
Convert a file's character encoding to UTF-8 and overwrite the original file with the -w --overwrite options:
# To convert character code to UTF-8:
nkf -w --overwrite <file>
Converting to EUC-JP
Convert a file's character encoding to EUC-JP:
# To convert character code to EUC-JP:
nkf -e --overwrite <file>
Converting to Shift-JIS
Convert a file's character encoding to Shift-JIS:
# To convert character code to Shift-JIS:
nkf -s --overwrite <file>
Converting to ISO-2022-JP
Convert a file's character encoding to ISO-2022-JP:
# To convert character code to ISO-2022-JP:
nkf -j --overwrite <file>
Newline Conversions
NKF can standardize newline characters. Use -Lu for LF (Unix), -Lw for CRLF (Windows), and -Lm for CR (Classic Mac OS):
# To convert newline to LF:
nkf -Lu --overwrite <file>
# To convert newline to CRLF:
nkf -Lw --overwrite <file>
# To convert newline to CR:
nkf -Lm --overwrite <file>
MIME Encoding and Decoding
Perform MIME encoding and decoding for email compatibility:
# To MIME encode:
echo テスト | nkf -WwMQ
# To MIME decode:
echo "=E3=83=86=E3=82=B9=E3=83=88" | nkf -WwmQ
Further Resources
For more detailed information and advanced usage, refer to the official documentation: