Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translating NULLS to Spaces

Status
Not open for further replies.

Dagon

MIS
Jan 30, 2002
2,301
GB
I receive some files from a mainframe application which contain NULL characters (ASCII zero). I want to be able to convert these to spaces but am having difficulty finding anything that can do it. I tried:

tr '\00' ' '

but that just seemed to remove the null character altogether. The application I'm using to load the file then fell over because all the characters had been shifted one position to the left. I also tried to write an awk script to do the same thing, but didn't have any success with that either.
 
Perhaps this ?
tr '\000' '\040'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
No, unfortunately I get exactly the same thing.
 
This works for me:
tr '\000' '\040' < /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I ran the command you suggest. Before the command, vi tells me there are 67124 characters with 174 nulls. After the command, it tells me there are 66950 characters with no nulls. So it looks as though, rather than be translated, the characters have simply been stripped out altogether.
 
I guess it's just a way of counting the characters that Dagon's using, rather than wc -c or whatever.
 
Yes, I was just using it to illustrate the point that there were fewer characters after the "tr" than before, so the translation could not have worked correctly.

I've managed to come up with a solution using an awk script:

{
outline = "";
for (i = 1; i <= length($0); ++i)
{
if (match(substr($0,i,1), "[A-Z0-9 .]") == 0)
{
outline = outline " ";
}
else
{
outline = outline substr($0,i,1);
}
}
print outline
}

I couldn't find an easy way to identify the null in awk. Looking for empty string ("") or \000 didn't work, so I just did it by excluding all the characters I knew could be in the file.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top