LoginSignup
27
13

More than 1 year has passed since last update.

Binary file (standard input) matches

Last updated at Posted at 2019-12-23

事象 : テキストファイルをgrepしたら怒られた

  • 環境
    • Windows10 Pro 64bit
    • grep (GNU grep) 3.1
$ grep hoge text_file.txt
Binary file (standard input) matches

原因 : grepが最初の数バイトとを見てバイナリファイルだと思ったから

Linuxのgrep検索で「バイナリー・ファイル(標準入力)は一致しました」と表示される原因と解決方法 - r_nobuホームページ

対応方法 : -aオプションをつけてテキストファイルであることを明示する

$ grep -a hoge text_file.txt
hoge
fugahoge

事象 : S3のファイル一覧を日付でgrepしたら怒られた

$ aws s3 ls s3://pon-bucket/dir-name/ --recursive | grep -e 2022-12-27
Binary file (standard input) matches

原因 : ファイル名に日本語があるから

-aオプションをつければgrepできますが文字化けもする。

$ aws s3 ls s3://pon-bucket/dir-name/ --recursive | grep -a -e 2022-12-27
2022-12-27 00:02:41     169069 dir-name/2022_12_27_□\□/sub-dir/chovin.epub
2022-12-27 00:02:39      19919 dir-name/2022_12_27_□□□□/□}□□□K.xlsx

対応 : -aオプションをつけて更に日本語に戻す

文字化けはiconvnkfなど文字コードを変換できるコマンドで戻してあげる。

$ aws s3 ls s3://pon-bucket/dir-name/ --recursive | grep -a -e 2022-12-27 | iconv -f sjis
2022-12-27 00:02:41     169069 dir-name/2022_12_27_予約/sub-dir/chovin.epub
2022-12-27 00:02:39      19919 dir-name/2022_12_27_早期/マンガ.xlsx
27
13
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
27
13