LoginSignup
132
124

More than 3 years have passed since last update.

Linux ファイルの文字コード確認・変換

Last updated at Posted at 2018-05-02

文字コード、改行コード変換する際、
毎回忘れてしまうので備忘録としてまとめてみました

【文字コード確認方法】

nkf --guess ファイル名
file -i ファイル名

【文字コード変換】

1.vi/vimの場合

:set fileencoding=文字コード
:set fenc=文字コード

2.コマンドの場合 iconv/nkf

  • iconv

    • フォーマット:iconv -f ファイルの文字コード -t 変換させる文字コード 対象ファイル
    • サンプル: iconv -f SHIFT_JIS -t UTF-8 shift_jis.csv > utf-8.csv
  • nkf

    • フォーマット: nkf 変換させる文字コード shift_jis.csv
    • サンプル: nkf -w shift_jis.csv > utf-8.csv
      • -w(utf-8), -s(shift_jis), --overwrite(上書きoption)

【ログをutf-8で閲覧】

tail -f sample.log | nkf -u -w

【一括置換】

  • 拡張子がtxtのすべてのファイルをUTF-8に一括変換する
  • find . -name '*.txt' -type f -print0 | xargs -0 nkf -u --overwrite -w
  • ※作業前にはバックアップを。。!
132
124
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
132
124