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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM