How to grep for presence of specific hex bytes in files?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 08:09:06

问题


My web app is displaying some bizarro output (unicode characters that shouldn't be there, etc.). The best I can reckon is that somehow I introduced a bad char somewhere in the source, but I can't figure out where.

I found this answer that states I can do something like:

grep -obUaP "<\x-hex pattern>" .

When I copy the unicode char out of the browser and into my Bless hex editor, it tells me that the exact bytes of the char are:

15 03 01 EF BF BD 02 02

How can I format <\xhex pattern> to match the exact bytes that I need. I tried:

grep -obUaP "<\x-15 03 01 EF BF BD 02 02>" .

But that doesn't work. Thoughts?


回答1:


Check the post again. FrOsT is not including the '<' and '>' in his actual grep command. He only used the carats to enclose an example statement. His actual statement looks like this:

"\x01\x02"

not:

"<\x01\x02>"

I have a C source file on my computer that begins with the line:

#include <stdio.h>

When I run

grep -obUaP '\x69\x6E\x63\x6C\x75\x64\x65' io.c

I get

1:include

That is, the line number followed by only the string matching the pattern.

You may want to run

man grep

and find out what all those options mean.



来源:https://stackoverflow.com/questions/23695609/how-to-grep-for-presence-of-specific-hex-bytes-in-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!