Removal of ^M added by a PC editor by a Unix editor

Removal of ^M added by a PC editor by a Unix editor

Post by Alex Cic » Tue, 02 Apr 2002 14:33:03



Hi,

Thank you very much for your kind help.  This is sort of a stupid
question.

I am porting some of source codes edited on PC to unix.  The character
^M is appended at the end of most lines, see below.  I am thinking of
removing them.

The character, appearing to be ^M, can only be view in vi, not in
pico, and it is one character because it can be deleted by command "x"
in vi.  When I tried to search Ctrl-M or just character M in vi, the
character ^M could not be matched.  It must be some special character.
 I wonder if this annoying character can be automatically replaced.

Thank you very much for your input.

Alex

PS:
Samples of the ^M (Here it is two characters, ^ and M)
int Set::remove(int val)
{       ^M
        if(!contains(val)){^M
                cout<<"\nThis element is not in the set! "^M
                        <<"Nothing to remove. Try another
one."<<endl;^M
        }^M
        else^M
                return l.remove(val); ^M

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Artur Roytbur » Tue, 02 Apr 2002 14:38:30


It is a special character.
To find it, search for Ctrl-v m.
To replace it, use sed:
sed 's/.$//' file > file.out
Artur


Quote:> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

> Thank you very much for your input.

> Alex

> PS:
> Samples of the ^M (Here it is two characters, ^ and M)
> int Set::remove(int val)
> {       ^M
>         if(!contains(val)){^M
>                 cout<<"\nThis element is not in the set! "^M
>                         <<"Nothing to remove. Try another
> one."<<endl;^M
>         }^M
>         else^M
>                 return l.remove(val); ^M


 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Andreas K?h? » Tue, 02 Apr 2002 15:11:30


Submitted by "Alex Cicco" to comp.unix.shell:
Quote:> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

[cut]

Unix uses <LF> as newline character, DOS/Windows uses <CR><LF>.

On some systems there is a command called "dos2unix" that will
make the conversion for you.  There is also a "unix2dos" program
on some of those systems.  Other systems might have a command
called "fromdos" (and "todos").

You can easily remove all <CR> from a file using 'tr' in Unix:

tr -d '\r' < oldfile > newfile

The following should also work:

tr -d '\015' < oldfile > newfile

(inspect newfile and copy it over to oldfile).

In Vi:

    :%:s/^M//g

Type the "^M" as <Cntl>-V <Cntl>-M.

For more info, search the web for "unix dos text conversion".

--
Andreas K?h?ri
----------------------------------------------------------------
NetBSD: Because you're worth it.    http://www.netbsd.org/

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by at\" » Tue, 02 Apr 2002 15:21:12



> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

Yes, you need to refer to it as \r in search string.

--
Alex Mitrofanov
Brainbench MVP for Unix Programming
hrrp://www.brainbench.com

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Ben Hearsu » Tue, 02 Apr 2002 15:50:54



> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

> Thank you very much for your input.

> Alex

> PS:
> Samples of the ^M (Here it is two characters, ^ and M)
> int Set::remove(int val)
> {       ^M
>         if(!contains(val)){^M
>                 cout<<"\nThis element is not in the set! "^M
>                         <<"Nothing to remove. Try another
> one."<<endl;^M
>         }^M
>         else^M
>                 return l.remove(val); ^M

look for a program called 'dos2unix'.
--
- ben
 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Floyd Davidso » Tue, 02 Apr 2002 16:23:14



>Hi,

>Thank you very much for your kind help.  This is sort of a stupid
>question.

>I am porting some of source codes edited on PC to unix.  The character
>^M is appended at the end of most lines, see below.  I am thinking of
>removing them.

>The character, appearing to be ^M, can only be view in vi, not in
>pico, and it is one character because it can be deleted by command "x"
>in vi.  When I tried to search Ctrl-M or just character M in vi, the
>character ^M could not be matched.  It must be some special character.
> I wonder if this annoying character can be automatically replaced.

>Thank you very much for your input.

This always brings on a half dozen or so ways to clean up the
text.  tr, sed, awk, vi, emacs, dos2unix, flip and so on.

The original, and arguably the best, way is simple,

   col  <input >output

The col program was intended to remove odd carriage controls from
text files.  You don't need to remember which regular expression,
which options or whatever.

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Joshua C. Bergero » Wed, 03 Apr 2002 04:54:38


One thing that I have found that works well, is merely opening the file
which has the ^M's in pico, and saving / closing the file.

After you do this, open the file in vi; the ^M characters should be gone.

Josh


Quote:> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

> Thank you very much for your input.

> Alex

> PS:
> Samples of the ^M (Here it is two characters, ^ and M)
> int Set::remove(int val)
> {       ^M
>         if(!contains(val)){^M
>                 cout<<"\nThis element is not in the set! "^M
>                         <<"Nothing to remove. Try another
> one."<<endl;^M
>         }^M
>         else^M
>                 return l.remove(val); ^M

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Jukka-Pekka Suomine » Wed, 03 Apr 2002 05:07:00



Quote:> One thing that I have found that works well, is merely opening the file
> which has the ^M's in pico, and saving / closing the file.

The command dos2unix should do the same thing IIRC..

--
Jukka-Pekka Suominen
?bo Akademi - MNF/IB

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Stefaan A Eeckel » Wed, 03 Apr 2002 06:31:10


On Mon, 1 Apr 2002 23:07:00 +0300



> > One thing that I have found that works well, is merely opening the
> > file which has the ^M's in pico, and saving / closing the file.

> The command dos2unix should do the same thing IIRC..

Or of course, in vi:

:g/^V^M$/s///

where ^V is ctrl-V and ^M ctrl-M...

--
Stefaan
--
Microsoft treats IT managers the way Proctor & Gamble treats nine-year-old
prospective consumers: lots of noise, bright colors, and jumping around.
Other software vendors just wish they could be so successful.
                                         -- Cameron Laird in comp.lang.tcl

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by laura fairhe » Wed, 03 Apr 2002 07:45:19



Quote:>Hi,

>Thank you very much for your kind help.  This is sort of a stupid
>question.

>I am porting some of source codes edited on PC to unix.  The character
>^M is appended at the end of most lines, see below.  I am thinking of
>removing them.

>The character, appearing to be ^M, can only be view in vi, not in
>pico, and it is one character because it can be deleted by command "x"
>in vi.  When I tried to search Ctrl-M or just character M in vi, the
>character ^M could not be matched.  It must be some special character.
> I wonder if this annoying character can be automatically replaced.

It's a control character;

^M = ctrl + M = CR (carriage return) = ascii 0x0D, octal 015, decimal 13

MSDOS systems represent the end of a line in text files as CR,LF
in the binary image file.

If you transport these files to UNIX without conversion, due to
the fact that UNIX text files are terminated with a single LF character
the CR shows up as the last character on each line.

Whenever possible transfer files using 'ftp' ensuring that the
mode is set to 'ascii', because then 'ftp' will take care of
transposing the end of line character for you from the remote
systems format into the local systems format.

To change these files from the command line you could use 'tr'
to delete any CR character occuring in the file;

$ tr -d '\15' <file.old >file.new

Or use 'sed' to delete the last character on each line;

$ sed 's/.$//' file.old >file.new

Within 'vi' you can use the ex-mode 's' command on all lines;

:%s/.$//

'vim' (default on linux) it depends on what the options settings
are because it tries to hide these things from you. But changing
the fileformat option and saving will probably work;

:set fileformat=unix
:wq

Quote:>Thank you very much for your input.

>Alex

>PS:
>Samples of the ^M (Here it is two characters, ^ and M)
>int Set::remove(int val)
>{       ^M
>        if(!contains(val)){^M
>                cout<<"\nThis element is not in the set! "^M
>                        <<"Nothing to remove. Try another
>one."<<endl;^M
>        }^M
>        else^M
>                return l.remove(val); ^M

byefrom

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492

 
 
 

Removal of ^M added by a PC editor by a Unix editor

Post by Rob Thor » Thu, 04 Apr 2002 17:47:30



> Hi,

> Thank you very much for your kind help.  This is sort of a stupid
> question.

> I am porting some of source codes edited on PC to unix.  The character
> ^M is appended at the end of most lines, see below.  I am thinking of
> removing them.

> The character, appearing to be ^M, can only be view in vi, not in
> pico, and it is one character because it can be deleted by command "x"
> in vi.  When I tried to search Ctrl-M or just character M in vi, the
> character ^M could not be matched.  It must be some special character.
>  I wonder if this annoying character can be automatically replaced.

> Thank you very much for your input.

> Alex

> PS:
> Samples of the ^M (Here it is two characters, ^ and M)
> int Set::remove(int val)
> {       ^M
>         if(!contains(val)){^M
>                 cout<<"\nThis element is not in the set! "^M
>                         <<"Nothing to remove. Try another
> one."<<endl;^M
>         }^M
>         else^M
>                 return l.remove(val); ^M

In emacs do M-x set-buffer-file-coding-system then undecided-unix
You could bind it to a key.