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

后端 未结 11 1674
遇见更好的自我
遇见更好的自我 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:00

    Here's what I used in a system that didn't have "strings" command installed

    cat yourfilename | tr -cd "[:print:]"
    

    This prints the text and removes unprintable characters in one fell swoop, unlike "cat -v filename" which requires some postprocessing to remove unwanted stuff. Note that some of the binary data may be printable so you'll still get some gibberish between the good stuff. I think strings removes this gibberish too if you can use that.

提交回复
热议问题