Operating System - HP-UX
1834395 Members
1481 Online
110066 Solutions
New Discussion

replace control characters in a file

 
Mark Smith T-Systems
Frequent Advisor

replace control characters in a file

does anybody now the vi command to remove a lot of ^M's in a large file and replace them with nothing ?
17 REPLIES 17
Robert-Jan Goossens_1
Honored Contributor

Re: replace control characters in a file

Hi Mark,

You can use the dos2ux command, have a look at the man page.

# dos2ux old_name > new_name

Regards,
Robert-Jan
LucianoCarvalho
Respected Contributor

Re: replace control characters in a file

hi,

Try to use dos2ux command.

#dos2ux file > newfile.

Regards
James R. Ferguson
Acclaimed Contributor

Re: replace control characters in a file

Hi Mark:

You can do:

# dos2ux file

# perl -pe 's%\r\n%\n%; s%\032%% if eof' file

Regards!

...JRF...
OldSchool
Honored Contributor

Re: replace control characters in a file

sounds like the file got ftp'd from Windows (or saved from Windows on a CIFS share)

dos2ux badfile > newfile

or in vi
s/$^V^M/g
Mark Smith T-Systems
Frequent Advisor

Re: replace control characters in a file

I would like to do this within vi if possible, the last reply doesnt seem to work
LucianoCarvalho
Respected Contributor

Re: replace control characters in a file


Try this in vi.

:%s/^V^M//g

LucianoCarvalho
Respected Contributor

Re: replace control characters in a file

I forgot to mention.
The ^V character is just for vi let you to type ^M command. The final command result in vi will lok like ":%s/^M//g"

Regards
Hein van den Heuvel
Honored Contributor

Re: replace control characters in a file


Hmmm, why not dos2ux?
Are those carriage returns in the missle of the lines?

Anyway, in vi you can use
:1,$s/^M//

That ^M must be entered as control-V control-M

hth,
Hein.
Hein van den Heuvel
Honored Contributor

Re: replace control characters in a file

Oops,

If there is more than 1 ^M on any line, then you'd need to add the 'g' at the end of the substitute command of course:

:1,$s/^M//g

That ^M must still be entered as control-V control-M

hth,
Hein.
OldSchool
Honored Contributor

Re: replace control characters in a file

sorry....didn't explain it well
OldSchool
Honored Contributor

Re: replace control characters in a file

sorry, didn't explain it well.

assuming the ones you want are at the end of the line, you want

1,$s/cntrl-Vcntrl-M$//g

the Cntrl-V "escapes" the Cntrl-M so it's taken as a literal, and the "$" in the pattern is an anchor for the end-of-line
Mark Smith T-Systems
Frequent Advisor

Re: replace control characters in a file

I am getting other characters in the file besides the ^M that shouldnt be there, the dos2ux takes out the ^M but not the others, I think the problem may be in the way the file was ftp transfered.
OldSchool
Honored Contributor

Re: replace control characters in a file

what kind of characters are you seeing that don't belong?

such things as Ctrl-L (form-feed) are retained for printing page breaks, which you may still want.

usually what happens is a file is created in Windows as text. Transferring this file in binary mode to a Unix box results in the Windows-style CR/LF pairs being retained, while an ascii-mode transfer translates them.

Without knowing what the file contains / how created, I would hesitate to alter it further.
Mark Smith T-Systems
Frequent Advisor

Re: replace control characters in a file

OldSchool

I got the ^M's out now butv it is putting this in:

if sum -r </dev/null >/dev/null 2>&1

instead of:
if sum -r /dev/null 2>&1


and I assume other lines are probably
corrupt like that too.
Mark Smith T-Systems
Frequent Advisor

Re: replace control characters in a file

there are other problems with this patch i downloaded, thanks fo r all of your assistance.
Dennis Handly
Acclaimed Contributor

Re: replace control characters in a file

>I got the ^M's out now but it is putting this in:
if sum -r </dev/null >/dev/null 2>&1

These are html formatting chars. You might want to use the right button to save the patch directly?
Muthyala
Frequent Advisor

Re: replace control characters in a file

Hi

Can you please let me know how to solve this issue.Currently i am facing same problem all of a sudden after server reboot .I am using CIFS/Samba to map my unix folders to windows.

TIA
Sri