Linux——grep binary file

原创声明:本文系博主原创文章,转载或引用请注明出处。

 

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可选值及含义为:

  1. binary(默认参数): 对二进制文件进行匹配但是不输出结果;
  2. without-match: 不对二进制文件进行匹配;
  3. text: 将所有文件当作文本文件进行匹配,这与-a参数的控制行为一致。

 

 

【参考资料】

  1. https://blog.csdn.net/xiayu98020214/article/details/49071241

 

posted @ 2019-07-23 16:44  怪猫佐良  阅读(5225)  评论(0编辑  收藏  举报