Solved

^M

Posted on 2001-07-11
9
378 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 20 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

820 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