120

I have a text file containing unwanted null characters (ASCII NUL, \0). When I try to view it in vi I see ^@ symbols, interleaved in normal text. How can I:

  1. Identify which lines in the file contain null characters? I have tried grepping for \0 and \x0, but this did not work.

  2. Remove the null characters? Running strings on the file cleaned it up, but I'm just wondering if this is the best way?

2

9 Answers 9

151

I’d use tr:

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

If you are wondering if input redirection in the middle of the command arguments works, it does. Most shells will recognize and deal with I/O redirection (<, >, …) anywhere in the command line, actually.

17
  • 12
    Actually, I believe it should be tr -d '\000' < file-with-nulls > file-without-nulls since < is part of the shell pipe functionality and not tr.
    – Mikael S
    Mar 7, 2010 at 23:50
  • 12
    Most shells will recognize & deal with < or > anywhere in the argument string, actually. Surprised me too.
    – pra
    Mar 8, 2010 at 18:16
  • 1
    +1 For usage of input redirection instead of cat |. A fine, clean solution and it solved my problem. Feb 13, 2014 at 7:14
  • 1
    This is an order of magnitude slower than sed for me Oct 30, 2017 at 4:06
  • 4
    @Pointy '\000' is used in lieu of '\0' in the POSIX opengroup specification for tr. That is a good reason to prefer it May 31, 2018 at 2:45
86

Use the following sed command for removing the null characters in a file.

sed -i 's/\x0//g' null.txt

this solution edits the file in place, important if the file is still being used. passing -i'ext' creates a backup of the original file with 'ext' suffix added.

6
  • 7
    Note: In FreeBSD (and I believe also Mac OS X), sed -i requires an extension in the next argument, but it may be empty. In those systems, add a '', as in: sed -i '' 's/\x0//g "$FILE".
    – Tim Čas
    Feb 1, 2017 at 21:05
  • 3
    This is an order of magnitude faster than tr for me Oct 30, 2017 at 4:06
  • For me, using Git for Windows and $ sed --version -> sed (GNU sed) 4.7, I had to use the following invocation to get a backup file called example.csv.bak: sed -i.bak 's/\x0//g' example.csv Jan 22, 2020 at 18:21
  • 1
    @TimČas you did it great, just missed one ' so it should be sed -i '' 's/\x0//g' some_file.xml
    – Dark
    Apr 29, 2020 at 7:29
  • On mac this only did the first null character and not all of them. gsed did work to do all of them.
    – phyatt
    Sep 8, 2021 at 18:36
23

A large number of unwanted NUL characters, say one every other byte, indicates that the file is encoded in UTF-16 and that you should use iconv to convert it to UTF-8.

2
  • 1
    I ran out of disk space while my application was logging. This resulting in these characters.
    – dogbane
    Mar 7, 2010 at 23:21
  • For example, it works using this command: iconv -f UTF-16 -t UTF-8 file.
    – djule5
    Apr 27, 2020 at 15:30
10

I discovered the following, which prints out which lines, if any, have null characters:

perl -ne '/\000/ and print;' file-with-nulls

Also, an octal dump can tell you if there are nulls:

od file-with-nulls | grep ' 000'
6

If the lines in the file end with \r\n\000 then what works is to delete the \n\000 then replace the \r with \n.

tr -d '\n\000' <infile | tr '\r' '\n' >outfile
1
  • PS. If you find yourself in a Windows DOS shell, you can get the GNU/win32 versions of Unix commands from Sourceforge.net. I use them all the time. Check out "od" the octal dump command for analysing what's in a file...
    – wwmbes
    Jun 20, 2016 at 14:22
3

Here is example how to remove NULL characters using ex (in-place):

ex -s +"%s/\%x00//g" -cwq nulls.txt

and for multiple files:

ex -s +'bufdo!%s/\%x00//g' -cxa *.txt

For recursivity, you may use globbing option **/*.txt (if it is supported by your shell).

Useful for scripting since sed and its -i parameter is a non-standard BSD extension.

See also: How to check if the file is a binary file and read all the files which are not?

2

I used:

recode UTF-16..UTF-8 <filename>

to get rid of zeroes in file.

0

I faced the same error with:

import codecs as cd
f=cd.open(filePath,'r','ISO-8859-1')

I solved the problem by changing the encoding to utf-16

f=cd.open(filePath,'r','utf-16')
0

Remove trailing null character at the end of a PDF file using PHP, . This is independent of OS

This script uses PHP to remove a trailing NULL value at the end of a binary file, solving a crashing issue that was triggered by the NULL value. You can edit this script to remove all NULL characters, but seeing it done once will help you understand how this works.

Backstory
We were receiving PDF's from a 3rd party that we needed to upload to our system using a PDF library. In the files being sent to us, there was a null value that was sometimes being appended to the PDF file. When our system processed these files, files that had the trailing NULL value caused the system to crash.

Originally we were using sed but sed behaves differently on Macs and Linux machines. We needed a platform independent method to extract the trailing null value. Php was the best option. Also, it was a PHP application so it made sense :)

This script performs the following operation:

Take the binary file, convert it to HEX (binary files don't like exploding by new lines or carriage returns), explode the string using carriage return as the delimiter, pop the last member of the array if the value is null, implode the array using carriage return, process the file.

//In this case we are getting the file as a string from another application. 
// We use this line to get a sample bad file.
$fd = file_get_contents($filename);

//We trim leading and tailing whitespace and convert the string into hex
$bin2hex = trim(bin2hex($fd));

//We create an array using carriage return as the delminiter
$bin2hex_ex = explode('0d0a', $bin2hex);

//look at the last element.  if the last element is equal to 00 we pop it off
$end = end($bin2hex_ex);
if($end === '00') {
   array_pop($bin2hex_ex);
}

//we implode the array using carriage return as the glue
$bin2hex = implode('0d0a', $bin2hex_ex);

//the new string no longer has the null character at the EOF
$fd = hex2bin($bin2hex);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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