Operating System - HP-UX
1833437 Members
3083 Online
110052 Solutions
New Discussion

Re: stripping control characters from line !!!!

 
SOLVED
Go to solution
Henrique Silva_3
Regular Advisor

stripping control characters from line !!!!

Now, I have done this in vi before but have not done in a long time, so, here is my questions. How do you strip control character from the end of the line on a file ? This happens usually when you ftp a file back and forth from a windows machine to unix.

so, you will have file such as :

line 1^M
line 2^M

For the life of me, I tried all kinds of sed commands, inside vi, such as
:1,$s/\^M//g

etc, but nothing seem to work. What kills me is that I had the same problem some 10 months ago and did it without thinking !!! I must be getting old :-(((

A search on google or looking at my vi book did not help either :-(((

Can somebody shed some light out there ???

Thanks

Henrique
"to be or not to be, what was the question ???? "
6 REPLIES 6
Michael Tully
Honored Contributor
Solution

Re: stripping control characters from line !!!!

Hi,

If your copying files back and forth
from a dos box, there are two utilities
that are available for this. The first
is 'dos2ux' which is for files coming
from dos to Unix. The second is 'ux2dos'
which of course goes from unix to dos.

You could use the vi method or even sed
but why bother when you can use these.

HTH
-Michael
Anyone for a Mutiny ?
Darrell Allen
Honored Contributor

Re: stripping control characters from line !!!!

Hi Henrique,

First thing, always ftp text files in ASCII mode. That way you don't have to be concerned with whether or not the files have the LF-CR in Windows and only LF (actually referred to as NL - newline) in UNIX.

If you do get a text file in UNIX from Windows that was transferred in binary mode, simply run dos2ux file >newfile

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
S.K. Chan
Honored Contributor

Re: stripping control characters from line !!!!

Craig Rants
Honored Contributor

Re: stripping control characters from line !!!!

Inside of your vi session enter

:%s/^M//g

where ^ is cntl-v and M is cntl-shift-M

and tada... gone.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Henrique Silva_3
Regular Advisor

Re: stripping control characters from line !!!!

Thanks for the replies, especially the last one, which gave me the sed syntax. That was really what I was looking for, but will take a look at the utilities to convert files back in forth. I was not aware of them.

Henrique
"to be or not to be, what was the question ???? "
Craig Rants
Honored Contributor

Re: stripping control characters from line !!!!

Henrique,
No comprende. No apunta para mi? Excusa mi espanol.

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut