Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

^M

Posted on 2001-07-11
9
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:interiot
ID: 6274281
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
ID: 6274737
cat file|sed "s/^M//g">anotherfile
mv anotherfile file
0
 
LVL 4

Expert Comment

by:prashant_n_mhatre
ID: 6274955
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Expert Comment

by:yuzh
ID: 6275402
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
 
LVL 5

Author Comment

by:Peewee
ID: 6276240
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
ID: 6277097
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
ID: 6277176
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 80 total points
ID: 6277871
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
ID: 6299003
super!

many thanks
peewee
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

670 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