Saturday, April 9, 2022

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

Issue

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?


Solution

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.



Answered By - chacewells
Answer Checked By - Senaida (WPSolving Volunteer)