Related to another question, in order to fuzzily detect binary files, is there a way to detect ␀ bytes in sed?
-
In GNU sed, yes, but note that on many unices, text utilities are not capable of handling null bytes.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2012-04-19 01:04:23 +00:00Commented Apr 19, 2012 at 1:04
Add a comment
|
2 Answers
Example:
Prove I'm sending a NUL byte, followed by a newline:
$ echo -e \\0 | hexdump -C
00000000 00 0a |..|
00000002
Now I change the NUL byte to an ! exclamation mark:
$ echo -e \\0 | sed 's/\x00/!/' | hexdump -C
00000000 21 0a |!.|
So the trick is using \x00 as NUL-byte.
Yes, the pattern \x00 matches to the null byte.
Example:
$ printf "\0\n\0\n" > file
$ sed -e 's/\x00/test/' -i file
$ cat file
test
test
$
-
1@l0b0: The reason, why it worked for me, was that I used zsh. According to POSIX, it replaces
\0by the zero byte. This replacement is not necessary ("shall be supported"), and actually bash does not support it directly. For it, you need to useecho -e. I replaceechowith withprintfin my answer which seems to be more compatible...jofel– jofel2012-04-18 12:04:45 +00:00Commented Apr 18, 2012 at 12:04