Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
Hi, I got some files that I need to process and they contain some lines like this: READ_CHARACTER^M_DONE I checked and this "^M" thing makes me problem when I try ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    3

    How to get rid of the insane "^M"

    Hi,

    I got some files that I need to process and they contain some lines like this:

    READ_CHARACTER^M_DONE

    I checked and this "^M" thing makes me problem when I try to input this file to some program. If edit manually with vi and remove the "^M", then no problem.

    But this file (and following) contains thousand of random appearances of this "^M". I thought about how to remove this. Perhaps sed -e "s/^M//g" could work, but I don't know how to use it.

    Any suggestion ?

    Thanks

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Use Backslash ( \ ) before ^ to override its default behavior :
    Code:
    sed -e "s/\^M//g" <file_name>
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    There's a way to delete those guys without leaving vi:

    Code:
    :%s/<control-V><control-M>//g
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by devils_casper View Post
    Use Backslash ( \ ) before ^ to override its default behavior :
    Code:
    sed -e "s/\^M//g" <file_name>
    It isn't necessary to use a backslash and the g option is redundant because every line has one ^M character at the end of the line, this is suffice:

    Code:
    sed 's/^M//' file > newfile
    however.... the ^M you must type as <Ctrl-v> and <Ctrl-m>.

    Regards

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    It isn't necessary to use a backslash and the g option is redundant because every line has one ^M character at the end of the line
    I think hefeweizen's example shows otherwise:
    Code:
    READ_CHARACTER^M_DONE
    If there can be one ^M embedded in the middle of the line, there can be more than one.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Hi Friends !

    I know a little about Scripting and try my hands sometimes.
    Learning a lot from you all. Thanx...
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  7. #7
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by wje_lf View Post
    If there can be one ^M embedded in the middle of the line, there can be more than one.
    You're right! There can be more in the middle of a line, the g flag is needed so my line should be:

    Code:
    sed 's/^M//g' file > newfile
    Quote Originally Posted by devils_casper View Post
    Hi Friends !

    I know a little about Scripting and try my hands sometimes.
    Learning a lot from you all. Thanx...
    Don't worry about that, we all learn from each other!


    Regards

  8. #8
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Here's yet another way to paint that bike shed:
    Code:
    $ dos2unix file_here
    Default mode converts the file and writes output to it.

  9. #9
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    The perl way:
    Code:
    perl -pi -e 's/^M//' filename

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    The perl way:
    Code:
    perl -pi -e 's/^M//' filename
    Of course, in that the ^M means "control vee, control em", as it did in the previous uses of it in this thread.

    The more conventional way in Perl is this, and in it you actually type all the characters as shown here. I've added back in the g, as noted in previous posts in this thread.
    Code:
    perl -pi -e 's/\r//g' filename
    --
    Bill

    Old age and treachery will overcome youth and skill.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...