Solved

^M

Posted on 2001-07-11
9
374 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Audit HISTFILE for all users on AIX 9 107
Where to get php 5.6 for AIX 7.1? 5 81
OpenLDAP Proxy to Active Directy 6 211
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 74
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

920 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

12 Experts available now in Live!

Get 1:1 Help Now