LoginSignup
18
12

More than 5 years have passed since last update.

文字コードを指定してgrepする。

Last updated at Posted at 2015-08-18

今時EUCのファイルなんて殆ど使わないが、grepせざるをえなくなったのでメモ。
コンソールはUTF8なのでそのままだとgrepできない。
ただ、やることは単純でnkfで検索したい文字をeuc-jpにしてファイルをgrep。
そして結果をUTF8にしているだけ。
ただし、LANG=ja_JP.eucjpにしないとeuc-jpでgrepができないので注意。
ちなみにgrepのバージョンは2.6。

LANG=ja_JP.eucjp grep -rn `echo "あたたたた" | nkf -e` * | nkf -w

manには下記の様に書いてあって LC_ALL、LC_なんちゃら、LANGの順で優先的にロケールが使われるらしい。

ENVIRONMENT VARIABLES
    The behavior of grep is affected by the following environment variables.

    The locale for category LC_foo is specified  by  examining  the  three  environment
    variables  LC_ALL,  LC_foo, LANG, in that order.  The first of these variables that
    is set specifies the locale.  For example, if LC_ALL is not set, but LC_MESSAGES is
    set  to  pt_BR,  then  the  Brazilian Portuguese locale is used for the LC_MESSAGES
    category.  The C locale is used if none of these environment variables are set,  if
    the  locale  catalog  is  not  installed, or if grep was not compiled with national
    language support (NLS).

nkf使わないでeuc-jpでシェルスクリプトを書いて実行する手もある。
shift_JISはLANG=ja_JP.sjis, nkf -sにすれば動く。多分。

18
12
0

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
18
12