How to grep a text file which contains some binary data?

后端 未结 11 1616
遇见更好的自我
遇见更好的自我 2020-11-30 19:48

grep returns

Binary file test.log matches

For example

echo    \"line1 re \\x00\\r\\nline2\\r\\nline3 re\\r\\n\" > test.log  # in zsh         


        
相关标签:
11条回答
  • 2020-11-30 20:18

    You could run the data file through cat -v, e.g

    $ cat -v tmp/test.log | grep re
    line1 re ^@^M
    line3 re^M
    

    which could be then further post-processed to remove the junk; this is most analogous to your query about using tr for the task.

    0 讨论(0)
  • 2020-11-30 20:19

    grep -a will force grep to search and output from a file that grep thinks is binary. grep -a re test.log

    0 讨论(0)
  • 2020-11-30 20:21

    You can force grep to look at binary files with:

    grep --binary-files=text
    

    You might also want to add -o (--only-matching) so you don't get tons of binary gibberish that will bork your terminal.

    0 讨论(0)
  • 2020-11-30 20:21

    you can do

    strings test.log | grep -i
    

    this will convert give output as a readable string to grep.

    0 讨论(0)
  • 2020-11-30 20:23

    You can use "strings" to extract strings from a binary file, for example

    strings binary.file | grep foo
    
    0 讨论(0)
提交回复
热议问题