Solved

^M

Posted on 2001-07-11
9
379 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
Technology Partners: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
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.
Suggested Courses

752 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