[comp.unix.wizards] replacing nulls in a file with sed.

monty@delphi.uchicago.edu (Monty Mullig) (07/25/90)

sorry to ask what i'm sure is a simple question, but i can't find
this information in the documentation and experimentation has led
nowhere.

i have a file that comes with null characters randomly placed in it.
i'd like to replace those nulls with spaces, but i can't seem to get
sed to do this.  the command:

sed 's/\(^@\)/ /g' < infile > outfile

removes the nulls, but doesn't replace them with spaces.
in emacs, the null characters show up as ^@ characters and
global search and replace works (using the control-q to enter
^@ as a true control character, rather than a carat and an
at sign).

thanks in advance,

--monty mullig
  university of chicago
  biological sciences division
  monty@delphi.bsd.uchicago.edu
  312 702-9130

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/26/90)

In article <1990Jul24.224811.22143@midway.uchicago.edu> monty@delphi.uchicago.edu (Monty Mullig) writes:
: i have a file that comes with null characters randomly placed in it.
: i'd like to replace those nulls with spaces, but i can't seem to get
: sed to do this.  the command:
: 
: sed 's/\(^@\)/ /g' < infile > outfile
: 
: removes the nulls, but doesn't replace them with spaces.
: in emacs, the null characters show up as ^@ characters and
: global search and replace works (using the control-q to enter
: ^@ as a true control character, rather than a carat and an
: at sign).

Most Unix utilities are not null-proof, because C doesn't make it real
easy to be null-proof.  Chances are the null isn't even making it past your
shell!  Even tr can't handle \000, seemingly.  GNU emacs is a fortunate
exception--so's Perl:

	perl -pe 's/\0/ /g' <infile >outfile

or, a bit more efficiently

	perl -pe 'y/\0/ /' <infile >outfile

Note also that you can specify the null in human-readable form with Perl.
So you can sneak it right past your shell.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov