Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Here is the beginning of a file:
# hexdump -n 550 myFile
0000000 f0f2 f5f0 f7f9 f1f1 f1f0 f0f0 e3f1 f3c8
0000010 f3f5 0000 0000 000c 0000 0000 0000 000c
0000020 0000 0c00 0000 0000 0000 0c00 0000 0000
0000030 000c 0000 0000 0000 000c 0000 0c00 0000
0000040 0000 0000 0c00 0000 0000 000c 0000 0000
0000050 0000 000c 0000 0c00 0000 0000 0000 0c00
0000060 0000 0000 000c 0000 0000 0000 000c 0000
00000b0 0000 0000 000c 0000 0000 0000 0000 0000
00000c0 0000 0000 0000 0c00 0000 0000 0000 0c00
00000d0 0000 0000 000c 0000 0000 0000 000c 0000
00000e0 0c00 0000 0000 0000 0c00 0000 0000 000c
00000f0 0000 0000 0000 000c 0000 0c00 0000 0000
0000100 0000 0c00 0000 0000 000c 0000 0000 0000
0000110 000c 0000 0c00 0000 0000 0000 0c00 0000
0000120 0000 0000 0c00 0000 0000 0000 0c00 0000
0000160 0000 0000 0c00 0000 0000 0000 0000 0000
0000170 0000 0000 0000 0000 000c 0000 0000 0000
0000180 000c 0000 0c00 0000 0000 0000 0c00 0000
0000190 0000 000c 0000 0000 0000 000c 0000 0c00
00001a0 0000 0000 0000 0c00 0000 0000 000c 0000
00001b0 0000 0000 000c 0000 0c00 0000 0000 0000
00001c0 0c00 0000 0000 000c 0000 0000 0000 000c
00001d0 0000 0000 0000 000c 0000 0000 0000 000c
0000210 0000 0000 0000 000c 0000 0000 0000 0000
0000220 0000 0000 0a00
0000226
in which we can see the hex values 0c and 0a
I don't understand why grep finds 0c but not 0a:
# grep -P '\x0c' myFile
Fichier binaire myFile correspondant
# grep -P '\x0a' myFile
<nothing in the output>
I am using CentOS.
–
–
–
–
\x0a isn't just any hex value - it's the hex value corresponding to the ASCII linefeed character.
Since grep is (by default) line-based, the linefeed characters are stripped out before pattern matching takes place. At least with GNU grep, you can change this behavior with the -z option:
-z, --null-data
Treat input and output data as sequences of lines, each
terminated by a zero byte (the ASCII NUL character) instead of a
newline.
however note that this will strip out ASCII nulls, so that you will no longer be able to grep for those.
–