Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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
share|improve this answer
4  
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
1  
shouldn't it be "tr -d '\000' < file-with-nulls > file-without-nulls" ? –  Seamus Abshere Jul 12 '13 at 19:32
    
Does it matter? –  Tom Wijsman Jul 12 '13 at 23:10

tr tripped over some other bytes in my file and sed didn't replace anything. I ended up doing it Python:

f = open('file-with-0bytes.dump')
for l in f.readlines():
  print l.replace("\0", '')

I also noticed some commands kinda leave the 0 bytes there but you can't see them anymore at least not in OSX. I used hexdump to debug this.

share|improve this answer

To match a null byte, I use this regex with Cygwin's SED:

[^\x01-\x7F]

share|improve this answer

It's quite easy to use Perl to perform a regex. Just replace sed with perl -np -e:

$ printf 'one\0two\0' | perl -np -e 's/\0/\n/g'
one
two

With the -n option, regexes are run line by line, just like sed.

If you want to use zero bytes as record separators, use Perl's -0 option.

$ printf 'one\0two\0' | perl -np0 -e 's/^/prefix /; s/\0/\n/g'
prefix one
prefix two
$ printf 'one\0two\0' | perl -np -e 's/^/prefix /; s/\0/\n/g'
prefix one
two

You can look up the command-line options of Perl by running perldoc perlrun.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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