Removing ^M character from text

Discussion in 'Ruby' started by John Victor, Nov 28, 2008.

  1. John Victor

    John Victor Guest

    I have some text that I just realized is screwing everything up because
    it contains the ^M character (control-M). I have tried using all kinds
    of regexp expressions to find and remove it but it is evasive :)

    What is the best way to find and remove the pesky ^M character from
    text?

    vic
    --
    Posted via http://www.ruby-forum.com/.
     
    John Victor, Nov 28, 2008
    #1
    1. Advertising

  2. John Victor wrote:
    > What is the best way to find and remove the pesky ^M character from
    > text?


    text.delete!("\r")
    or if you prefer:
    text.delete!("\C-M")

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 28, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    Hi John,
    str = "123^M56"
    newstr = str.sub(/^M/, '')

    Note - for the substitution, you enter the '^M' character using Ctrl+v+m (-
    or at least I do on Solaris.)

    ~mm

    On Fri, Nov 28, 2008 at 2:57 PM, John Victor <> wrote:

    > I have some text that I just realized is screwing everything up because
    > it contains the ^M character (control-M). I have tried using all kinds
    > of regexp expressions to find and remove it but it is evasive :)
    >
    > What is the best way to find and remove the pesky ^M character from
    > text?
    >
    > vic
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Michael Monaghan, Nov 28, 2008
    #3
  4. John Victor

    Robert Dober Guest

    On Fri, Nov 28, 2008 at 8:57 PM, John Victor <> wrote:
    > I have some text that I just realized is screwing everything up because
    > it contains the ^M character (control-M). I have tried using all kinds
    > of regexp expressions to find and remove it but it is evasive :)
    >
    > What is the best way to find and remove the pesky ^M character from
    > text?
    >
    > vic
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    A simpler method, applicable if the c-m is coming from line ends

    ruby -ne 'puts chomp' xxx > yyy

    HTH
    Robert


    --=20
    Ne baisse jamais la t=EAte, tu ne verrais plus les =E9toiles.

    Robert Dober ;)
     
    Robert Dober, Nov 28, 2008
    #4
  5. John Victor

    John Victor Guest

    Thanks alot everyone...I got it working.

    I never realized how badly ^M was screwing me up all these years when
    trying to match with regular expressions. I wish there was a way to see
    all the end of the line characters, carriage returns and new line
    without having to guess if they exist and using trial and error for
    hours when doing regexp matches.

    vic

    --
    Posted via http://www.ruby-forum.com/.
     
    John Victor, Nov 29, 2008
    #5
  6. John Victor

    Tim Hunter Guest

    John Victor wrote:
    > Thanks alot everyone...I got it working.
    >
    > I never realized how badly ^M was screwing me up all these years when
    > trying to match with regular expressions. I wish there was a way to see
    > all the end of the line characters, carriage returns and new line
    > without having to guess if they exist and using trial and error for
    > hours when doing regexp matches.
    >
    > vic
    >


    On Linux, OS X, and other *ix systems, you can use cat -v. In Ruby,
    String#inspect will show special characters escaped, so you can just use

    p str

    to see special characters. A newline shows up as "\n", for example.

    --
    RMagick: http://rmagick.rubyforge.org/
     
    Tim Hunter, Nov 29, 2008
    #6
  7. John Victor

    John Victor Guest

    Thanks for the String#inspect, that really helps.
    I am trying to do a regexp match on the first line only of this text, I
    used the output from text.inspect:

    "CPU utilization for five seconds: 4%/1%; one minute: 2%; five minutes:
    2%\r\n"
    "PID QTy PC Runtime (ms) Invoked uSecs Stacks TTY
    Process\r\n"
    "1 Cwe 40E8BAAC 80 1574 50 5560/6000 0 Chunk
    Manager \r\n"
    "2 Csp 41AD34D0 107060 19504749 5 2600/3000 0 Load Meter
    \r\n"
    "3 Mwe 41E1244C 8 8260 0 5136/6000 0 MPLS MIB
    traps \r\n"
    "4 Mwe 403D9860 1580 812729 1 5544/6000 0 DHCPD
    Timer \r\n"
    "5 Lst 40E9F268 211247688 17501448 12070 5616/6000 0 Check
    heaps \r\n"

    I thought it would be as simple as:

    /^CPU\sutilization.*\r\n/

    but this returns all the text I have shown above. Why is it that my
    regexp doesn't stop at the "\n"?

    thanks

    vic




    Tim Hunter wrote:
    > On Linux, OS X, and other *ix systems, you can use cat -v. In Ruby,
    > String#inspect will show special characters escaped, so you can just use

    --
    Posted via http://www.ruby-forum.com/.
     
    John Victor, Nov 29, 2008
    #7
  8. John Victor

    Robert Dober Guest

    Robert Dober, Nov 29, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    617
  2. Mike
    Replies:
    9
    Views:
    850
    Oliver Wong
    Mar 13, 2006
  3. bruce
    Replies:
    6
    Views:
    832
    Simon Forman
    Jul 4, 2006
  4. Chris  Chiasson
    Replies:
    6
    Views:
    627
    Richard Tobin
    Nov 14, 2006
  5. Andyza
    Replies:
    4
    Views:
    319
    Anthony Jones
    Feb 17, 2006
Loading...

Share This Page