Link to home
Start Free TrialLog in
Avatar of prasen120998
prasen120998

asked on

How to remove ^M characters from a file

There Control M(^M) characters in a file along with blank spaces
ie. "                                        ^M"
     <----------blank spaces ----->
I need to remove all the lines in the file where there are blank spaces with ^M and also new lines with ^M characters.

Pls help.

prasen
Avatar of prasen120998
prasen120998

ASKER

The OS is AIX 4.3, its a .txt file
tr -d \013 < badfile > goodfile
Avatar of ramazanyich
dos2unix your_in_file > out_file
out_file will be without ^M
For AIX, you see the answer in:
you can use:

 sed s/^M// file > newfile
mv newfile file


also see
http:Q_21000845.html

to learn more.
ASKER CERTIFIED SOLUTION
Avatar of bytta
bytta
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yuzh: a bit lazy ;-)

 sed '/^M/d' file > newfile

# where ^M is a real carriage return, depending on your shell. Most likely  Ctrl-V followed by Ctrl-J works
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
^m is same \013

^M is "\015" !

eg:

tr -d "\015" < infile > outfile
Not trying to beat a dead horse.  The "tr" method seems best.  Here's a script: "stripCR":

#!/bin/sh
if [ ! $# = 1 ] ;then echo "Usage: stripCR {filename}..." ;exit ;fi
cFile=$1
if [ ! -f $cFile ] ;then echo "No such file..." ;exit ;fi
if tr -d '\r' < $cFile > ./_$$.tmp ;then
  mv ./_$$.tmp $cFile
fi
# End...

^M is CR (Carriage return) so '\r' will work.

I personally think that dos2unix is still the best command to use to remove the ^M automatically.

Usage: dos2unix inputfile outputfile

Input file and Output file can be the same file or different files.
here are 2 methods

sed -e 's/^M$//' xxx.log > xxx.lognew && mv -f xxx.lognew xxx.log

for x in *; do
mv $x $x.old
sed -e 's/^M$//' $x.old > $x
rm -f $x.old
done

Hope this helps

Dan

12/06/2004 05:18PM PST
Gives more efficient command line ...
Gheist -

Thanks for your comment. The two answers I recommended are the only ones that treated the part of the question about removing lines containing only blanks. As I read the question yet again, it looks like the lines containing only blanks are to be removed entirely. I would question whether either of the two recommended responses actually does what the asker wanted, but it seems pretty clear that those responses offering various ways of stripping out just the \r characters fall short.

jmcg
EE Cleanup Volunteer
No-one removes empty lines ... ( like grep '^..*' or so (this is wrong..)  )
vim example is same as using sed, aix's vi is able to use it. ( oslevel = 4.3.3-9 )

Question is a bit messy.
> I need to remove all the lines in the file where there are blank spaces with ^M and also new lines with ^M characters
Common sense tells that leaving only lines with one single word is impractical at best, and it all looks like a conversion of PC-DOS file to AIX.
Maybe if no one cares ...