What is the sed incantation to remove null bytes from a file? I'm trying:

s/\000//g

but that is stripping out strings of zeroes.

s/\x00//g

seems to have no effect. I'm trying to do this in a sed script, so I'm not sure the "echo" trick will work.

link|improve this question
feedback

1 Answer

I don't know how you can exactly achieve this with sed, but this is a solution that works with tr:

tr < file-with-nulls -d '\000' > file-without-nulls

This is a solution for sed that works in some occasions but not all:

sed 's/\x0//g' file1 > file2

This is a solution that involves replacing into space characters which should work in all occasions:

sed 's/\x0/ /g' file1 > file2
link|improve this answer
2  
that looks like a very incomplete answer. why would it work on some occasions and not others, and if so then wouldn't an example be useful? – barlop May 24 '11 at 19:11
@barlop: Because of the way it is implemented? The OP didn't specify one and I'm not going to enumerate every single implementation... – Tom Wijsman May 24 '11 at 20:50
2  
Well that then sounds fine to me, so you're saying it depends on the implementation of SED. If you hadn't said that you'd have left open the possible suggestion that one implementation of SED may remove nulls from one file and not another file, depending on the data in the file. – barlop May 24 '11 at 22:03
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.