How to filter out control characters (^M) in unix file using VI or VIM

How to filter out control characters (^M) in unix file using VI or VIM

Post by Kevin » Wed, 02 Apr 2003 05:59:07



Hi,

Is there a slick way of editing out control characters, specifically
^M, using VI or VIM?  The background is that I edited some files in
Windows using Wordpad, and I believe it added ^M to the end of each
line, and now my scripts don't work in unix on some computers (yet on
others it does!!!).  I've edit files on wordpad and transplanted them
to unix with no problem before.  But now this is problem.  I'm
assuming it's the added control characters that are the problem.
Please advise!  Also, if anyone can recommend a good VI/ VIM/ Unix
cookbook that helps in these areas, that would be great.  Thanks in
advance!

Kevin R.

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by Edu » Wed, 02 Apr 2003 06:05:02


Hi,

        If all lines have the ^M char at the end:

        [ESC]:%s/.$/

--------------
Edu Rodrguez


> Hi,

> Is there a slick way of editing out control characters, specifically
> ^M, using VI or VIM?  The background is that I edited some files in
> Windows using Wordpad, and I believe it added ^M to the end of each
> line, and now my scripts don't work in unix on some computers (yet on
> others it does!!!).  I've edit files on wordpad and transplanted them
> to unix with no problem before.  But now this is problem.  I'm
> assuming it's the added control characters that are the problem.
> Please advise!  Also, if anyone can recommend a good VI/ VIM/ Unix
> cookbook that helps in these areas, that would be great.  Thanks in
> advance!

> Kevin R.


--
---------------------------------
Eduardo Rodrguez Castillo

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by rico » Wed, 02 Apr 2003 06:13:06



Quote:> Hi,

> Is there a slick way of editing out control characters, specifically
> ^M, using VI or VIM?  The background is that I edited some files in
> Windows using Wordpad, and I believe it added ^M to the end of each
> line, and now my scripts don't work in unix on some computers (yet on
> others it does!!!).  I've edit files on wordpad and transplanted them
> to unix with no problem before.  But now this is problem.  I'm
> assuming it's the added control characters that are the problem.
> Please advise!  Also, if anyone can recommend a good VI/ VIM/ Unix
> cookbook that helps in these areas, that would be great.  Thanks in
> advance!

Does "man dos2unix" produce anything that rings a bell?

Rico.

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by Edu » Wed, 02 Apr 2003 06:05:59


Sorry, ;-)

This is the good one :

        [ESC]:%s/.$//


> Hi,

>         If all lines have the ^M char at the end:

>         [ESC]:%s/.$/

> --------------
> Edu Rodrguez

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by Joseph Seig » Wed, 02 Apr 2003 06:43:27



> Hi,

> Is there a slick way of editing out control characters, specifically
> ^M, using VI or VIM?  The background is that I edited some files in
> Windows using Wordpad, and I believe it added ^M to the end of each
> line, and now my scripts don't work in unix on some computers (yet on
> others it does!!!).  I've edit files on wordpad and transplanted them
> to unix with no problem before.  But now this is problem.  I'm
> assuming it's the added control characters that are the problem.
> Please advise!  Also, if anyone can recommend a good VI/ VIM/ Unix
> cookbook that helps in these areas, that would be great.  Thanks in
> advance!

Try "dos2unix", the counterpart to "unix2dos".  You can also control-V
in vi to escape the ^M character so you can type vi commands to modify
or delete it, but there are some vi variants that hide the
^M characters so this won't work in those cases.

Joe Seigh

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by Fredrik Roube » Wed, 02 Apr 2003 07:45:50



Quote:> Is there a slick way of editing out control characters, specifically
> ^M, using VI or VIM?

Vim can automatically load files with DOS line endings. Just load the file
into Vim like this:

:e ++ff=dos file

You could also make Vim recognize different line ending conventions
automatically:

:set ffs=unix,dos,mac

Before writing the file again, you must tell Vim that you wish to use a
different convention than the one used when loading the file:

:set ff=unix

Finally, to answer your question directly; there is a slick way to edit
out control characters. Press Ctrl-V before the control character in order
to input the control character literarly. Then you can do like this to
delete all Ctrl-M:s at the end of the lines:

:%s/^M$//

This is especially useful in files using mixed line ending conventions
(that can't be loaded automatically by Vim).

Cheers // Fredrik Roubert

--
M?llev?ngsv?gen 6c  |  +46 46 188127
SE-222 40 Lund      |  http://www.df.lth.se/~roubert/

 
 
 

How to filter out control characters (^M) in unix file using VI or VIM

Post by Kevin » Thu, 03 Apr 2003 00:41:47




> > Is there a slick way of editing out control characters, specifically
> > ^M, using VI or VIM?

> Vim can automatically load files with DOS line endings. Just load the file
> into Vim like this:

> :e ++ff=dos file

> You could also make Vim recognize different line ending conventions
> automatically:

> :set ffs=unix,dos,mac

> Before writing the file again, you must tell Vim that you wish to use a
> different convention than the one used when loading the file:

> :set ff=unix

> Finally, to answer your question directly; there is a slick way to edit
> out control characters. Press Ctrl-V before the control character in order
> to input the control character literarly. Then you can do like this to
> delete all Ctrl-M:s at the end of the lines:

> :%s/^M$//

> This is especially useful in files using mixed line ending conventions
> (that can't be loaded automatically by Vim).

> Cheers // Fredrik Roubert

Thanks a lot guys -I will try these methods!
Kevin
 
 
 

1. remove control characters from a text file using vi

I am trying to remove control characters from a text file using vi.
I have experimented with the [:cntrl:] class-char but I can't seem
to make to work in a search.

If you have another method for removing control characters, I'd like
to hear that too.

Thankx
Jon

2. HELP: Silly man pages !

3. Unix Libraries Ins/Outs

4. Microsoft to fight for your digital rights

5. How do I display (hidden) control characters in a file with vi

6. IBCS-compile-problems

7. French Characters using vi editor in SCO Unix

8. stty SANE flag.

9. More page outs than page ins?

10. Control characters shelling into vi from man

11. CONTROL Characters & VI

12. Filtering remote control-characters

13. need lpr filter to handle F77 carriage control characters