replacing nulls in a file with sed.

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Thu Jul 26 06:58:23 AEST 1990


In article <1990Jul24.224811.22143 at midway.uchicago.edu> monty at 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 at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.wizards mailing list