Replace ^Z Character

Hello,

I am trying to replace all ^Z chars in my file to a space using the following command:

cat <file> | tr '\32' ' '

But the above command is deleting all NULL in the file. Hence the field positions are being changed. Please let me know the reason, if anyone knows.

Thanks in advance !!!

This works for me:

$ tr '\32' ' ' < file1 > file2
$ cat -v file1
A C 6 T^Z
C^ZB 4 R
A B 3 T^Z
D E 5 4

$ cat -v file2
A C 6 T 
C B 4 R
A B 3 T 
D E 5 4

Still, the NULL is also getting deleted (\0). How can i delete only 032 (^Z) ?

Hmm. That's not happening for me.

$ cat -v <(tr '\32' ' ' < file1)
A C^@ 6 T 
C B 4 R
A B 3 T 
D E 5 4

$ od -b file1
0000000   101 040 103 000 040 066 040 124 032 012 103 032 102 040 064 040
0000020   122 012 101 040 102 040 063 040 124 032 012 104 040 105 040 065
0000040   040 064 012                                                    
0000043

$ od -b file2
0000000   101 040 103 000 040 066 040 124 040 012 103 040 102 040 064 040
0000020   122 012 101 040 102 040 063 040 124 040 012 104 040 105 040 065
0000040   040 064 012                                                    
0000043

If you're outputting it to the terminal, that would eat it up, so you wouldn't see it.

did you try it in Solaris or Red hat ?

I 'tried it out' in OS X, and don't see why Red Hat or Solaris would be any different.

Hmm .. I was not sure so wanted to check with you. Can you please tell me how to insert a NULL in vi editor ?

I don't want to touch the production file.

CentOS 6.2

[root@centos62t scripts]# cat -v file1
A B C D^Z
A^@B C D
[root@centos62t scripts]# tr '\32' ' ' < file1
A B C D 
AB C D
[root@centos62t scripts]# cat -v <(tr '\32' ' ' < file1)
A B C D 
A^@B C D
[root@centos62t scripts]# od -b file1
0000000 101 040 102 040 103 040 104 032 012 101 000 102 040 103 040 104
0000020 012
0000021

Red Hat:

od -bc file1
0000000 141 040 142 040 000 040 144 040 032 012
             a          b          \0         d         032  \n

cat file1 | tr '\32' ' ' > file2

od -bc file2
0000000 141 040 142 040 000 040 144 040 040 012
             a          b          \0         d               \n

IT WORKS !!! Only the 032 got translated to 040. The 000 (\0) remains the same.

Solaris:

od -bc file1
0000000 141 040 142 040 000 040 144 040 032 012
             a          b          \0         d         032  \n

cat file1 | tr '\32' ' ' > file2

od -bc file2
0000000 141 040 142 040 040 040 144 040 040 012
             a          b                   d               \n

IT DOES NOT WORK. The 000 (\0) also got replaced to 040. Hence the NULL is getting changed to a space, though i give only ^Z (32) in command.

Please let me know your thoughts.

On Solaris try /usr/xpg6/bin/tr or /usr/xpg4/bin/tr instead of tr

It works for me without using xpg

# uname -a
SunOS sol10m 5.10 Generic_147441-01 i86pc i386 i86pc

# printf "A B C D\nE F G H^Z\nI\0J K L\n" > file1

# tr '\32' ' ' < file1 > file2

# diff file[12]                
2c2
< E F G H
---
> E F G H

The \0 might be getting replaced writing to the terminal, but not when writing to a file.

Scott -
Is your ^Z actually ASCII 26?

As shown in the previous od output, yes.

# od -b file1
0000000 101 040 102 040 103 040 104 012 105 040 106 040 107 040 110 032
0000020 012 111 112 040 113 040 114 012
0000030

The Null is the "issue" because the terminal doesn't display it. So it's not really an issue at all.

On Solaris 10, Sparc, I get:

$ printf '\000' | tr '\032' ' ' | od -bc
0000000
$ printf '\000' | /usr/xpg4/bin/tr '\032' ' ' | od -bc
0000000 000
          \0
0000001

Ahhh. I have a bunch of aliased stuff from xpg4, 6 and sfw.

I think I'll add some more :smiley:

/usr/xpg4/bin/tr command works.

Thank you all.