原創聲明:本文系博主原創文章,轉載或引用請注明出處。
grep命令是linux下常用的文本查找命令。當grep檢索的文件是二進制文件時,grep命令會提示:
$grep pattern filename
Binary file filename matches
如果此時確實需要對二進制文件進行匹配,可以加 -a 參數,使grep將二進制文件當作文本文件來進行匹配。
使用man命令查看grep參數,可以發現:
-a, --text Treat all files as ASCII text. Normally grep will simply print ``Binary file ... matches'' if files contain binary characters. Use of this option forces grep to output lines matching the specified pattern.
...
--binary-files=value
Controls searching and printing of binary files. Options are binary, the default: search
binary files but do not print them; without-match: do not search binary files; and text: treat all files as text.
即可以通過添加-a參數,使grep將所有文件以ascii格式來讀取。
也可以通過--binary-files=value參數來進行控制,value可選值及含義為:
- binary(默認參數): 對二進制文件進行匹配但是不輸出結果;
- without-match: 不對二進制文件進行匹配;
- text: 將所有文件當作文本文件進行匹配,這與-a參數的控制行為一致。
【參考資料】