Kbase 15005: How to remove CTRL-M (^M) from (DOS) ascii files.
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  5/10/1998 |
|
How to remove CTRL-M (^M) from (DOS) ascii files.
CAVEAT: Do not attempt this on binary files.
If you have a file that was transferred from a DOS system to a unix
system using ftp or rcp, it will have ^M characters on the end of each
line. These characters are not removed by quoter and result in
error 223 when read into non-character (integer, decimal) fields.
How you get rid of them depends on your access to the original file,
and the level of expertise of a user.
I. If you have access to the DOS system from which the file was trans-
ferred, simply repeat the transfer by specifying ascii mode. If
the transfer was done in binary because the files REALLY contains
binary data, see CAVEAT above.
II. If you have "inherited" this file, and cannot have it re-transfer-
red, then it will depend on how often you expect to have to do
this. If this is a one-shot deal, then here is a vi command
sequence that will remove the ^Ms:
vi filename
:%s/^M//g
:wq
The way you enter the ^M is by typing CTRL-V CTRL-M. This command
does a global substitution of all ^M with nothing; that is, delete
them. This same technique can be used to remove other control
characters such as ^L (form-feed) or ^G (bell).
III. If you expect to have to do this on a frequent basis, or if it
has to be done by a non-sophisticated user, you might prefer a
sed command file and a shell script:
You will still need to use vi to create the sed command file as
follows:
vi sed^M
is/^M//<esc>:wq
As above, ^M is entered by typing CTRL-V CTRL-M, and <esc> is the
escape key to terminate the insert. And now a shell script
called REMOVE-CTRL-M to make it all convenient.
#!/bin/sh
sed -f sed^M $1 > $1.out
sed -f sed^M $1 > $1.out
rm $1
mv $1.out $1
Now when you get one of these DOS files you simply type
REMOVE-CTRL-M <dosfile>
Similarly, you could REMOVE-CTRL-L or REMOVE-CTRL-G.
Disclaimer:
I am not a professional altho I play one on TV.
Do not attempt this at home unless accompanied by a
child under the age of 9.
Your milage may vary.
Progress Software Technical Support Note # 15005