Solved

^M

Posted on 2001-07-11
9
368 Views
Last Modified: 2012-05-04
I've got loads of ^M  charcters in my unix files.  

I believe they are windows line breaks which need to be removed.  Can someone suggest a way of getting rid of these ^M file by file apart from the obvious way of manually getting rid of them..

i'm looking for the easiest and most effective way.

many thanks
Peter
0
Comment
Question by:Peewee
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:interiot
Comment Utility
tr -d '\r' < filename > fixed_file
mv fixed_file filename


This deletes all occurances of '\r', or ^M.  'tr' usually translates a set of characters to another set of characters, but the -d makes it delete the first set instead.


On a solaris box I'm on, there's a dos2unix command as well, that may work if it's available to you.
0
 
LVL 6

Expert Comment

by:bira
Comment Utility
cat file|sed "s/^M//g">anotherfile
mv anotherfile file
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
Open file in VI

:1,$ s'^M''g

To get this '^M' in the command line:
Keep CTRL pressed and then press v and m

You need not to move the existing file.
0
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
Hi Peewee,

    You can use "dos2unix" command to convert the dos file into UNIX
format, in this case is get rid of ^M

    eg.  $dos2unix dos-filename newname
           mv newname dos-filename


     Cheers!

===========

     yuzh
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:Peewee
Comment Utility
ok, thnaks chaps,

i've tried both the sed and dos2unix operations and both appear to work ok file by file.

The qusetion begs begs if i got 100 files in a directory and thirty directories to do, how should i do this?

can either or both of these be extended to complete directories?

many thanks
Peter
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
Peewee if you want to do the same...then you may use the follwoing command in a shell program

strings file > newfile

replace file and newfile by appropriate arguments
0
 
LVL 3

Expert Comment

by:interiot
Comment Utility
First of all, here's a solution which edits files in place, and does it automatically, since no previous solutions fit this criteria:

    perl -pe 's/\r//g' -i filename

And a solution for your second problem:

    find . -print | xargs -n 1 perl -pe 's/\r//g' -i
0
 
LVL 38

Accepted Solution

by:
yuzh earned 20 total points
Comment Utility
Hi Peewee,

    You can use a text editor to put all the file names
including path to the file in a text file FILELIST:

    then do the followings:

    for i in `cat FILELIST`
    do
    dos2unix $i ${i}.unix
    mv $i ${i}.org
    mv ${i}.unix $i
    done

    The above command will do the job for you. It rename the original file to filename.org, and use the original file name for the UNIX format. you can delete all the dos
format file if you don't want to keep them by replace:
     mv $i ${i}.org
     to: rm $i

you can put the about command in a little script.

    Cheers!

=======
yuzh
0
 
LVL 5

Author Comment

by:Peewee
Comment Utility
super!

many thanks
peewee
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now