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
prasen120998Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prasen120998Author Commented:
The OS is AIX 4.3, its a .txt file
0
gheistCommented:
tr -d \013 < badfile > goodfile
0
ramazanyichCommented:
dos2unix your_in_file > out_file
out_file will be without ^M
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

yuzhCommented:
For AIX, you see the answer in:
you can use:

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


0
yuzhCommented:
also see
http:Q_21000845.html

to learn more.
0
byttaCommented:
I guess I'm the first one to get rid of the white spaces too...
# removes ^M and throws away all lines not containing any letter or number (probably what you want)
 sed s/^M// file | grep  [a-zA-Z0-9] > newfile

# Peronally I like to do it using vim (no newfile made) - but if I remember correctly this reduces
"                                        ^M" (spaces or tabs) to an empty line, but doesn't remove it
vi -e -c "%s/[ \t]*^M//g" -c "wq" -c 2 file1

Note that ^M isn't a "^" and a "M" but a meta char so you can't copy/paste it - create it in the command line by pressing Ctrl-V and then Enter

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahoffmannCommented:
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
0
sirjbCommented:
There is another way to remove all BLANK+^M at once

sed /[[:space:]]*[[:space:]]^M//g file > correctfile


The combination of [[:space:]]*[[:space:]]^M is actually: any combinations of any kind of spaces ( tabs, space, other blank ) end with control+M
This will not replace the control+M char but also all blank spaces in front of it.
remember the control +M => ctrl-V and then Enter.

If you need other combination using sed, let me know.

Hope this can help you.

Regards,

JB
0
gheistCommented:
^m is same \013

0
yuzhCommented:
^M is "\015" !

eg:

tr -d "\015" < infile > outfile
0
smiththomasgCommented:
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.

0
redtwilight04Commented:
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.
0
danrudolphCommented:
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

0
gheistCommented:
12/06/2004 05:18PM PST
Gives more efficient command line ...
0
jmcgOwnerCommented:
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
0
gheistCommented:
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.
0
gheistCommented:
Maybe if no one cares ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.