get rid of control char in text file

get rid of control char in text file

Post by Jianping Zh » Sat, 26 Jul 1997 04:00:00



Hi,

I have a large number of C source files, each with a control char '\032' sticking at the end of the file. Never mind how I got it.
The nature of the control char:
1)  when I compile the source code, 'cc' complains:
     No new-line char at end-of-file
     Unknown control character \032
2)  use vi to look at the file, last line appears to be '^Z'
3)  use cat, last line does not appear.

I tried         tr "[^Z]" "[\012]" < old > new
     and        tr "[\032]" "[\012]" < old > new
 to replace it with new line char, and I got "bad string" complaints

Obviously I can delete the last line with vi manually, but it looks silly.

Any suggestion of a shell script will be highly appreciated ?

J. Zhou
--

=============================================================
  http://irc.leeds.ac.uk/~phyjz/jianping.html
                     |  J. Zhou  |

=============================================================

 
 
 

get rid of control char in text file

Post by Per-Ivar Emanuelse » Sat, 26 Jul 1997 04:00:00



> I tried         tr "[^Z]" "[\012]" < old > new
>      and        tr "[\032]" "[\012]" < old > new
>  to replace it with new line char, and I got "bad string" complaints

Try
         tr "\032" "\012" < old > new
instead.

Per-Ivar
---------------------------------------
"Mr Worf, scan that ship." "Aye, Captain... 300 DPI?"

 
 
 

get rid of control char in text file

Post by Stefan `Sec` Ze » Sat, 26 Jul 1997 04:00:00



> Hi,

> I have a large number of C source files, each with a control char '\032' sticking at the end of the file. Never mind how I got it.
> The nature of the control char:
> 1)  when I compile the source code, 'cc' complains:
>      No new-line char at end-of-file
>      Unknown control character \032
> 2)  use vi to look at the file, last line appears to be '^Z'
> 3)  use cat, last line does not appear.

> I tried         tr "[^Z]" "[\012]" < old > new
>      and        tr "[\032]" "[\012]" < old > new

i think
tr '\032' '\012' < old > new

should do the trick :)

CU,
    Sec
--
Fuer die Raupe ist es das Ende der Welt,
Fuer den Rest der Welt ist es ein Schmetterling
                                                            Error 0: No error

 
 
 

get rid of control char in text file

Post by Mark Green » Sat, 26 Jul 1997 04:00:00



> Hi,

> I have a large number of C source files, each with a control char '\032' sticking at the end of the file. Never mind how I got it.
> The nature of the control char:
> 1)  when I compile the source code, 'cc' complains:
>      No new-line char at end-of-file
>      Unknown control character \032
> 2)  use vi to look at the file, last line appears to be '^Z'
> 3)  use cat, last line does not appear.

> I tried         tr "[^Z]" "[\012]" < old > new
>      and        tr "[\032]" "[\012]" < old > new
>  to replace it with new line char, and I got "bad string" complaints

> Obviously I can delete the last line with vi manually, but it looks silly.

> Any suggestion of a shell script will be highly appreciated ?

> J. Zhou
> --

> =============================================================
>   http://irc.leeds.ac.uk/~phyjz/jianping.html
>                      |  J. Zhou  |

> =============================================================

A couple of FYI's:

- The ctrl-z is the default end-of-file marker for DOS/Windows (all
releases). So "How you got it" is indeed quite relavent to your problem.

- "cat -v" will display control chacters in octal format

- "tr -c '[ -~]' '[A-_]'" will translate all non-printable ASCII
characters  (i.e. control chars) into corresponding printable equivalents
(see the examples in "man tr").  Using this you can eliminate all known
and unknown control chars in a file.  For a C program or shell script,
I'd recommend redirecting the output to temp file, and running diff on
the orginal vs. the temp so you can tell if the changes are going to
present other syntactical problems for your program.

Hope this helps.

--

The above opinions are mine, not my employer's.

 
 
 

get rid of control char in text file

Post by Adam Shor » Sat, 26 Jul 1997 04:00:00



> Hi,

> I have a large number of C source files, each with a control char
> '\032' sticking at the end of the file. Never mind how I got it.
> The nature of the control char:
> 1)  when I compile the source code, 'cc' complains:
>      No new-line char at end-of-file
>      Unknown control character \032
> 2)  use vi to look at the file, last line appears to be '^Z'
> 3)  use cat, last line does not appear.

> I tried         tr "[^Z]" "[\012]" < old > new
>      and        tr "[\032]" "[\012]" < old > new
>  to replace it with new line char, and I got "bad string" complaints

Try using vi's global replace, ie in vi type

:1,$s/<ctrl>v<ctrl>z//g

where <ctrl> is the control key

 
 
 

get rid of control char in text file

Post by Bill Marc » Sat, 26 Jul 1997 04:00:00




Quote:>Hi,

>I have a large number of C source files, each with a control char '\032' sticking at the end of the file. Never mind how I got it.
>The nature of the control char:
>1)  when I compile the source code, 'cc' complains:
>     No new-line char at end-of-file
>     Unknown control character \032
>2)  use vi to look at the file, last line appears to be '^Z'
>3)  use cat, last line does not appear.

>I tried         tr "[^Z]" "[\012]" < old > new
>     and        tr "[\032]" "[\012]" < old > new

Try it without the brackets:
tr "\032" "\012" < old > new

> to replace it with new line char, and I got "bad string" complaints

>Obviously I can delete the last line with vi manually, but it looks silly.

>Any suggestion of a shell script will be highly appreciated ?

>J. Zhou
>--

>=============================================================
>  http://irc.leeds.ac.uk/~phyjz/jianping.html
>                     |  J. Zhou  |

>=============================================================

--
Bill Marcum    bmarcum at iglou dot com
Quote:>YOU MUST CEASE AND DESIST YOUR ECLECTIC THIXOTROPERY.
>                                                -- K.
>                                                Not wearing a toupee.

 
 
 

get rid of control char in text file

Post by Joe Clun » Sun, 27 Jul 1997 04:00:00




> > Hi,

> > I have a large number of C source files, each with a control char '\032' sticking at the end of the file. Never mind how I got it.

How about dtox? I've run into similar problems with other files, and I
got
read of the ^Z with "dtox file > newfile"

--
Joe Clune                  | "In the beginning the Universe was created.

and
www2.netdoor.com/~joeclune |  been widely regarded as a bad move." D
Adams

 
 
 

get rid of control char in text file

Post by Toby » Mon, 28 Jul 1997 04:00:00


Quote:> > I tried         tr "[^Z]" "[\012]" < old > new
> >      and        tr "[\032]" "[\012]" < old > new
> >  to replace it with new line char, and I got "bad string" complaints

> Try using vi's global replace, ie in vi type

> :1,$s/<ctrl>v<ctrl>z//g

But you state that you have a *large* number of files
to perform this operation on.  Vi could become tedious.
I would look into your 'tr' idea some more.  I don't
have time to check, but I think that your second
example will work without the square brackets.

Then you would go:

        for each in *.c; do
                tr "\032" "\012" < $each > $each.fix
                #mv $each.fix $each
        done

Uncomment the 'mv' line when you know that this
is working properly.

Toby

        -There otta be a law-

 
 
 

get rid of control char in text file

Post by Floyd Davids » Tue, 29 Jul 1997 04:00:00




>>I have a large number of C source files, each with a control
>>char '\032' sticking at the end of the file. Never mind how
>>I got it.

>>The nature of the control char:
>>1)  when I compile the source code, 'cc' complains:
>>     No new-line char at end-of-file
>>     Unknown control character \032
>>2)  use vi to look at the file, last line appears to be '^Z'
>>3)  use cat, last line does not appear.

>>I tried         tr "[^Z]" "[\012]" < old > new
>>     and        tr "[\032]" "[\012]" < old > new

>Try it without the brackets:
>tr "\032" "\012" < old > new

The easy way:

   col -xb < old > new

--




 
 
 

get rid of control char in text file

Post by Peter Smulder » Tue, 29 Jul 1997 04:00:00



> A couple of FYI's:
> - The ctrl-z is the default end-of-file marker for DOS/Windows (all
> releases). So "How you got it" is indeed quite relavent to your problem.

Many Unix implementations have a conversion program call for this
purpose, called dos2ux, dos2unix, or similar.
--
Peter
 
 
 

get rid of control char in text file

Post by Jianping Zh » Wed, 30 Jul 1997 04:00:00


Quote:>>I tried         tr "[^Z]" "[\012]" < old > new
>>     and        tr "[\032]" "[\012]" < old > new
> try tr "\032" "\012" < old > new
> instead

.....

Thank you all for all these clever ideas. I have tried
 tr "\032" "\012" <old >new in a script, it works perfect. I will do the same thing with all the suggestions you provided first thing tomorrow morning, because I have already corrected all the files.

Quote:> - The ctrl-z is the default end-of-file marker for DOS/Windows (all
> releases). So "How you got it" is indeed quite relavent to your probl

Those files were indeed originally in my PC, ctrl-z only came up after I ftp the files to my unix machine. I did suspect that is the case at the beginning.

Thanks again

---jianping

--

=============================================================
  http://irc.leeds.ac.uk/~phyjz/jianping.html
                     |  J. Zhou  |

=============================================================

 
 
 

get rid of control char in text file

Post by Villy Kru » Sat, 02 Aug 1997 04:00:00



>Those files were indeed originally in my PC, ctrl-z only came up after I ftp the files to my unix machine. I did suspect that is the case at the beginning.

Hi,

Did you use ftp in binary mode?  If you ftp in ascii mode allyour ^m and ^z
should have been taken care of by the ftp program itself.  If im not mistaken
ftp in ascii mode will convert new-line to CRLF if you send from unix and
the receiving unix will convert back to NL characters.  If you send to DOS
the CRLF will of course be retained.  

Try abd see what happens.

Villy

 
 
 

get rid of control char in text file

Post by Michael P. Reil » Sat, 09 Aug 1997 04:00:00



: >Those files were indeed originally in my PC, ctrl-z only came up after
: I ftp the files to my unix machine. I did suspect that is the case at
: the beginning.

: Hi,

: Did you use ftp in binary mode?  If you ftp in ascii mode allyour ^m and ^z
: should have been taken care of by the ftp program itself.  If im not
: mistaken
: ftp in ascii mode will convert new-line to CRLF if you send from unix and
: the receiving unix will convert back to NL characters.  If you send to DOS
: the CRLF will of course be retained.  

: Try abd see what happens.

: Villy

This doesn't answer the question of deleting the characters once on UNIX.
Some systems have a "dos2unix" or "dos2ux" program, but they work
differently.

Assuming that you want to delete all ^m and ^z, then the easiest thing is
to redirect the file thru tr(1):
  mv file file.O; tr -d '\032\015' < file.O > file

If you just want to get rid of the cntl-Z at the end - the first line
that contains only that character (Bourne):
  cz=`echo x | tr x '\032'`
  mv file file.O; sed "/^${cz}$/d" file.O | tr -d '\015' > file

        -Arcege

 
 
 

1. Redirected text file has weird control chars.

When I do this:  

man xdm > textfile  (replace 'xdm' with any unix man pg. entry)

I get weird control characters in textfile. I'm using a vt100
terminal telnet'ed to a UNIX system (and using csh). How do I
get "just" a text file?

If I printed this file on a postscript printer, would these
characters be interpreted as underlines and bold-characters?

Thanks.

--
David M. Jackson
Georgia Institute of Technology, Atlanta Georgia, 30332
uucp:     ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!gt1801a

2. Installation troubles with RH and win95

3. Getting rid of text in a file

4. Slow File Copying

5. How to get rid of tty control chars?

6. Support non reserved ports for NFS client

7. Hot to get rid of control chars ?

8. lockd woes on Solaris 2.4

9. Getting RID of Access Control Lists (ACLs)

10. Apache: Getting rid of .sl suffix binding to text/vnd.wap.sl type

11. EOF: text or control char

12. How do I delete files w/control char embedded in file name?

13. getting rid of top lines of a file without searching through entire file