Operating System - HP-UX
1828985 Members
2327 Online
109986 Solutions
New Discussion

Remove ^M character from file

 
SOLVED
Go to solution
Nameesh
Frequent Advisor

Remove ^M character from file

Hi,

I ftp'ed few files from my Windows machine to HP-UX machine. But, the *.ksh files contain
^M character. How can I get rid of the characters ? Is there a software that can be installed on UX box to convert all the files ?

Regards,
Nameesh.
21 REPLIES 21
Oliver Charni
Trusted Contributor

Re: Remove ^M character from file

Hi!

There's a Utility which is called dos2unix which is able to convert these Files.

regards
oliver
if it smell's funny on the outside, it's worse on the inside
Michael Tully
Honored Contributor

Re: Remove ^M character from file

There is a tool to remove these:

# dos2ux uncoverted_file > converted_file

There is a man page for this as well

# man dos2ux
Anyone for a Mutiny ?
Nameesh
Frequent Advisor

Re: Remove ^M character from file

Hi,

Can you send me the link from where I can download the utility ?

Regards,
Nameesh.
Michael Tully
Honored Contributor

Re: Remove ^M character from file

It is part of the standard OS. it should be in /usr/bin or /usr/sbin

How else to find it:

# whereis dos2ux

# whence dos2ux

# find /usr -name dos2ux -print

Anyone for a Mutiny ?
Hoefnix
Honored Contributor

Re: Remove ^M character from file

When FTP is used to send ASCII files to a UNIX box, just send these files in ascii mode then you do not have to use the dos2unix tools.

ftp hostname
user password
ascii
put ascii.txt
quit

Hope this helps,

Regards,

Peter Geluk
Nameesh
Frequent Advisor

Re: Remove ^M character from file

Hi,

Thnx for the input. But, the problem with the do2ux is that we need to copy the file into another file ie.,

dos2ux file1 > file2

file2 has got all the ^M character removed. So, need to perfomr one more step of copying file2 to file1 to have the original file with no ^M.

Please advice script for same.

Regards,
Nameesh.
Michael Tully
Honored Contributor

Re: Remove ^M character from file

simply:

# dos2ux myfile >myfile

will convert the original file and not save a copy.
Anyone for a Mutiny ?
Sanjay Kumar Suri
Honored Contributor

Re: Remove ^M character from file

While transferring text file using ftp give ascii command to get rid of ^M character.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Nameesh
Frequent Advisor

Re: Remove ^M character from file

Hi,

When I do the operation as :

dos2ux file1 > file1

Size of "file1" becomes "0" and there is nothing in file1.

Regards,
Nameesh.
Sanjay Kumar Suri
Honored Contributor

Re: Remove ^M character from file

Do

dos2ux file1 > file2

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
David_246
Trusted Contributor

Re: Remove ^M character from file

Hi,

vi /tmp/file1

:1,$s/+v +m//g

This replaces all special character(+v) ^M with nothing, multiple times per line. If you would have searched this subject would have returned at least 20 results.

Regs David
@yourservice
Hoefnix
Honored Contributor

Re: Remove ^M character from file

did you try the FTP session using ascii mode? Then you won't need to convert dos2ux.

Regards,

Peter
V. Nyga
Honored Contributor

Re: Remove ^M character from file

Hi,

yes, 'dos2ux file1 > file1' doesn't work proper.
Do 'dos2ux file1 > file2' and 'mv file2 file1' - tested ;-)

Volkmar
*** Say 'Thanks' with Kudos ***
T G Manikandan
Honored Contributor
Solution

Re: Remove ^M character from file

cd
for i in `ls`
do
dos2ux $i >$i.new
mv $i.new $i
done
Fabio Ettore
Honored Contributor

Re: Remove ^M character from file

Hi,

Peter and Sanjay are right!!!
Sure, it is a text file and your problem just is that you transferred it in a type different than ASCII.
Anyway now you can use sed command to solve your problem, here like to do it by one command:

# sed 's/^M/ /' >

I hope this helps you.

Best regards,
Ettore
WISH? IMPROVEMENT!
Michael Schulte zur Sur
Honored Contributor

Re: Remove ^M character from file

Hi,

Michael:ouch.. the shell creates an outfile always before starting the command. So indeed the command saves nothing. ;-))

Otherwise ascii ftp or sed or use another outfile and then rename as already posted.

BTW: are the ^M allways at the end?

greetings,

Michael
Ravi_8
Honored Contributor

Re: Remove ^M character from file

Hi,

you have 2 ways to get rid of this
1. open the file using vi
press
:1,$s/ctrl+v ctrl+M//
:wq (to save the file)

2. using sed

sed 's/^M//g'



never give up
Nameesh
Frequent Advisor

Re: Remove ^M character from file

Hi All,

Thanx for the inputs. I wrote a script which I used to solve the problem :

dos2ux $1 > tmp
mv tmp $1

As suggested many of you, while ftp'g the file from the Windows box we can set "transfer type as ascii" and ^M character wouldn't be there.

Regards,
Nameesh.
Marton Ferenc
Advisor

Re: Remove ^M character from file

if you want to erase only ^M:

cat |tr -d \^M >
J5000
Jos de Ruiter
Advisor

Re: Remove ^M character from file

Hi,

You can find de program with:

# find . -name dos2ux -print

Groetjes.
Jos de Ruiter
Jos de Ruiter
Rory R Hammond
Trusted Contributor

Re: Remove ^M character from file


Their are many ways to do this. more than 3 that were shown. some better than others

The best way is to ftp the files correctly

OK here is MYLIST OF POSSIBLE solutions:

for arg in files*
do
strings $arg $arg.tmp
or
cat $arg |tr -d "\r" > $arg.tmp
or
dos2ux $arg > $arg.tmp
or
awk 'gsub(/\r$/,"")' $arg > $arg.tmp
or
sed 's/^M$//' $arg > $arg.tmp
# thats s/ctrl+v ctrl+M$//
mv $arg.tmp $arg
done


or

ed, ex and vi

for arg in files
do
echo "1,$ s/\r$//g\n.\nw\nq" |ed $arg
or
echo "1,$ s/\r$//g\nwq" |ex $arg
or
echo "^\1,$ s/\r$//g\nwq" |vi $arg
done

Some of the above deletes ^M at the end of line ignoring ^M that could be embeded in the middle (unlikely with text files)
to delete all ^M change \r$ to \r

Thanks that was fun!

Rory
There are a 100 ways to do things and 97 of them are right