Operating System - HP-UX
1752772 Members
5071 Online
108789 Solutions
New Discussion юеВ

Re: commad to remove control M character

 
SOLVED
Go to solution
Shivkumar
Super Advisor

commad to remove control M character

Sirs,

I have a file which has probably windows format text file. I want to remove ^M (control M) from the begining of the lines in the files.

In the second files i ^M are present in the begining of each lines and somewhere at the end of the lines.

Please suggest hpux command to remove the extra character ^M from both the files.

Thanks,
Shiv
11 REPLIES 11
Mel Burslan
Honored Contributor
Solution

Re: commad to remove control M character

Shiv,

Usually /usr/bin/dos2ux command does this for you if you have them and it comes with hpux v11.11.

if youare ftp ing these files, you can choose to transfer them in ascii form instead of binary, which will eliminate those pesky ^M characters.

Last but not the least, you can remove them using sed, albeit, I have not tested this solution but I do not see any reason for it not to work. Here it is:

cat SourceFile|sed -e "1,\$s/^M//" > OutputFile

to create the ^M on the line above you first hit ctrl-v then ctrl-m instead of typing it, to give it the actual meaning of the character.

Hope one of these solutions work for you...
________________________________
UNIX because I majored in cryptology...
Ralph Grothe
Honored Contributor

Re: commad to remove control M character

There are numerous solutions.

You could also use the translate command

tr -d \\015 < file > file_cr_stripped

You can also do the substitution while you are editing the file in vi

vi file

then type

Esc:%s/Ctrl+vCR//CR:wqCR

Esc: gets you in command mode
% refers to the whole file
s/// substitutes patternA by patternB
Ctrl+v escapes special keys like Carriage Return (CR) which appears as ^M
Since patternB is empty the CRs get stripped
:wq exits and saves from buffer to file
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor

Re: commad to remove control M character

Hi SHiv:

If you are running on HP-UX you have 'dos2ux' and 'ux2dos'.

As noted in their man pages, "dos2ux and ux2dos read each specified file in sequence and write it to standard output, converting to HP-UX format or to DOS format, respectively."

If you are *not* on a HP server, you can make strip the carriage returns (^M) with this:

# perl -ni.bak -e 's/\r\n/\n/s;s/\032//s;print' myfile

The above make a backup copy of "myfile" as "myfile.bak" and performs the conversion.

Regards!

...JRF...
Devender Khatana
Honored Contributor

Re: commad to remove control M character

HI,

I normally use dos2unix command to do this.

HTH,
Devender
Impossible itself mentions "I m possible"
Yogeeraj_1
Honored Contributor

Re: commad to remove control M character

hi,

this question has been asked so many times here. :)

please see:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=972302

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Arunvijai_4
Honored Contributor

Re: commad to remove control M character

Hi Shiv,

You can use the following methods to remove ^M from a file

In vi, :s/^M//g
Or use sed to do it:
sed 's(ctrl v ctrl m)g//g' old.file > new.file

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Sandman!
Honored Contributor

Re: commad to remove control M character

Ranjith_5
Honored Contributor

Re: commad to remove control M character

Hi Shiva,

There are many methods.

1.Using dos2ux command

#dos2ux file1>newfile

2.Using VI

Edit the file using vi, then
:%s/\^M//g

3.Using tr command.

#tr -d \\015 < filename > newfile

You may also use sed for the same purpose.

Regards,
Syam

Arunvijai_4
Honored Contributor

Re: commad to remove control M character

Hi Shiv, You can use strings utility that comes with HP-UX by default.

# strings > good_file

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"