Computing.Net > Forums > Unix > remove ^M from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

remove ^M from file

Reply to Message Icon

Name: sharathkv
Date: March 2, 2004 at 20:46:17 Pacific
OS: HPUX
CPU/Ram: 512
Comment:

Hi,

How to remove ^M character from a file.
A sample script would be helpful

thanks
sharath




Response Number 1
Name: e.diot
Date: March 2, 2004 at 23:58:59 Pacific
+1
Reply:

Hi,

in vi do the following:

:g/^M/s/// (the ctrl-m has to be entered with the key strokes "ctrl-v followed by ctrl-m". That's teh only way it works!

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

Bye!



Response Number 2
Name: aigles
Date: March 3, 2004 at 04:23:19 Pacific
+1
Reply:

And with 'tr' :

tr -d '\r' <old.file >new.file

Jean-Pierre.



Response Number 3
Name: nails
Date: March 3, 2004 at 09:06:46 Pacific
+2
Reply:

Hi:

You can also use the octal representatiion of ^M. The following gets rid of control-M as well as control-Z (DOS eof marker):

tr -d '\015\032' < file

You can also embed these sequences in sed:

sed 's/'"$(printf '\015')"'$//
s/'"$(printf '\032')"'$//' file

Regards,

Nails



Response Number 4
Name: cbailey33
Date: March 3, 2004 at 17:17:53 Pacific
+2
Reply:

One other way is to use the built-in
command. I am not sure if HP-UX has
this command, but with Solaris you
can just do:

dos2unix <dos file> <unix file>

You can also go back with unix2dos.




Response Number 5
Name: David Perry
Date: March 5, 2004 at 10:20:55 Pacific
+1
Reply:

HPUX has the command "dos2ux" instead of dos2unix. Save the following as a script "dos2unix" and it will work the same as other systems.

#!/bin/sh
tmpfile="/tmp/$$"
if [ $# -ne 1 ] ; then
echo "USAGE $0 dosfile"
exit 4
fi
dos2ux $1 > $tmpfile && mv $tmpfile $1



Related Posts

See More



Response Number 6
Name: bryanpayne
Date: April 18, 2004 at 23:29:39 Pacific
+1
Reply:

The best way is to avoid getting them in the file in the first place. The most common way this happens is that you ftp the file with the wrong option of Binary or Ascii. If you use Binary on an FTP it does nothing to the file, but if you use Ascii it attempts a conversion to UNIX, or DOS format. For example if you often ftp windows text files to unix you should use the Ascii mode for ftp so that it will strip off the ^M. If you don't know which option to use, just try one option, and if it doesn't work, then just retransfer the file using the other option and it should be fine.



Reply to Message Icon

expression problem anyone knows anything 'bo...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Google Ads



Results for: remove ^M from file

remove ^M from a file www.computing.net/answers/unix/remove-m-from-a-file/7537.html

How to Remove LILO from Windows NT boot www.computing.net/answers/unix/how-to-remove-lilo-from-windows-nt-boot-/2230.html

Removing LILO from Windows NT boot secto www.computing.net/answers/unix/removing-lilo-from-windows-nt-boot-secto/2229.html