Forum: Ruby Removing ^M character from text

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57ba42762c8d83fd5dc4ae76ec79ac8f?d=identicon&s=25 John Victor (victors)
on 2008-11-28 21:02
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
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-11-28 21:12
(Received via mailing list)
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
4a728bc64ea78b9b5dca98a4e81bb76e?d=identicon&s=25 Michael Monaghan (Guest)
on 2008-11-28 21:12
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-11-28 22:06
(Received via mailing list)
On Fri, Nov 28, 2008 at 8:57 PM, John Victor <john@sflistdb.com> wrote:
>
>
A simpler method, applicable if the c-m is coming from line ends

 ruby -ne 'puts chomp'  xxx > yyy

HTH
Robert


--
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert Dober ;)
57ba42762c8d83fd5dc4ae76ec79ac8f?d=identicon&s=25 John Victor (victors)
on 2008-11-29 01:26
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
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-11-29 02:12
(Received via mailing list)
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.
57ba42762c8d83fd5dc4ae76ec79ac8f?d=identicon&s=25 John Victor (victors)
on 2008-11-29 18:42
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-11-29 19:43
(Received via mailing list)
On Sat, Nov 29, 2008 at 1:21 AM, John Victor <john@sflistdb.com> wrote:
> Thanks alot everyone...I got it working.
>
Rubyquiz can help here ;)
http://splatbang.com/rubyquiz/quiz.rhtml?id=171_hexdump
Cheers
Robert



--
Ne baisse jamais la tête, tu ne verrais plus les étoiles.

Robert Dober ;)
This topic is locked and can not be replied to.