Help with one-liner

  • Thread starter Philip Mateescu
  • Start date
P

Philip Mateescu

Hello,

I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)
[i386-cygwin]).

My one liner sample:

$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();
puts(line + "---\n"); }'

I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"

---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms

What am I doing wrong? And why? :)

Thanks.



Best regards.
philip
 
A

Austin Ziegler

My one liner sample:

$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();
puts(line + "---\n"); }'

I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"

Using 1.8.1, I have no problems if I change it to:

ping localhost | ruby -e "$<.readlines.each { |line| puts
%Q(#{line.chomp}---) }"

#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.

-austin
 
G

Geoff Youngs

Philip said:
My one liner sample:

$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();
puts(line + "---\n"); }'

I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"
[snip]

What am I doing wrong? And why? :)

Probably line endings - windows line endings are "\r\n" - and chomp!
seems to only be removing
the "\n", so the carriage return is returning the cursor to the
beginning of the line before
trying to display the "---\n" string.

Try using line.rstrip! to remove all trailing whitespace, or
line.chomp!("\r\n").

Unless you want to process the entire output of the ping command at
once, using IO#each_line
might be better than IO#readlines. Also, the "\n" at the end of the
string isn't needed -
IO#puts is capable of adding one automatically.

HTH,


Geoff.
 
P

Philip Mateescu

Thanks.

However, I'm temporary stuck with 1.6.8; I get the same result using
your variation.

I think at this point I'm more curious as to why does it do it rather
than how to fix it :)



Austin said:
Using 1.8.1, I have no problems if I change it to:

ping localhost | ruby -e "$<.readlines.each { |line| puts
%Q(#{line.chomp}---) }"

#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.

-austin


Best regards.
philip
----
Lightning flashes,
Sparks shower,
In one blink of your eyes,
You have missed seeing.

---
 
A

Aredridel

Hello,

I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)
[i386-cygwin]).

My one liner sample:

$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();
puts(line + "---\n"); }'

I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"

---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms

What am I doing wrong? And why? :)

You're one windows, so the line ending coming out of ping is \r\n, not
\n -- chomp! isn't eating that, and leaving the carriage-return, but not
the linefeed. So it prints the whole line, returns to the start, prints
three dashes, then newlines and starts again.

Take two characters off the end instead of one and it should work.

Ari
 
P

Philip Mateescu

You're probably right about the line ending, here's a variation:

$ ping localhost | ruby -e '$<.each_line { |line| chomp1 = line.chomp();
chomp_rn = line.chomp("\r\n"); chomp_r = line.chomp("\r"); chomp_n =
line.chomp("\n"); puts chomp1 + "-- 1--"; puts chomp_rn + "--rn--"; puts
chomp_r + "-- r--"; puts chomp_n + "-- n--"; }'

And the result:
-- 1--from 127.0.0.1: bytes=32 time<1ms TTL=128
--rn--from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
-- r--
-- n--from 127.0.0.1: bytes=32 time<1ms TTL=128

Baffling....

Hello,

I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)
[i386-cygwin]).

My one liner sample:

$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();
puts(line + "---\n"); }'

I would expect it to put --- at the end of each line. Instead it
replaces the first 3 chars of each line with "---"

---
---ging nha-a30p-009.nha.corp [127.0.0.1] with 32 bytes of data:
---
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---ly from 127.0.0.1: bytes=32 time<1ms TTL=128
---
---g statistics for 127.0.0.1:
--- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
---roximate round trip times in milli-seconds:
--- Minimum = 0ms, Maximum = 0ms, Average = 0ms

What am I doing wrong? And why? :)


You're one windows, so the line ending coming out of ping is \r\n, not
\n -- chomp! isn't eating that, and leaving the carriage-return, but not
the linefeed. So it prints the whole line, returns to the start, prints
three dashes, then newlines and starts again.

Take two characters off the end instead of one and it should work.

Ari

Best regards.
philip
 
P

Philip Mateescu

Aredridel said:
Looks like the third one was close...

... but no cigar :)

Might try \n\r ... I can never remember which order it is.


I think I figured it out:
p line

" Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\r\n"

I don't know where that came from...





Best regards.
philip
 
M

Martin DeMello

Austin Ziegler said:
#puts automatically places a \n at the end of the line, and it's
better to use interpolation. The reason that I switched from -e '...'
to -e "..." is because I tested it on Windows.

Yes, I found that one out the hard way too (totally mystified me).

martin
 
S

Sean O'Dell

You're probably right about the line ending, here's a variation:

$ ping localhost | ruby -e '$<.each_line { |line| chomp1 = line.chomp();
chomp_rn = line.chomp("\r\n"); chomp_r = line.chomp("\r"); chomp_n =
line.chomp("\n"); puts chomp1 + "-- 1--"; puts chomp_rn + "--rn--"; puts
chomp_r + "-- r--"; puts chomp_n + "-- n--"; }'

And the result:
-- 1--from 127.0.0.1: bytes=32 time<1ms TTL=128
--rn--from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
-- r--
-- n--from 127.0.0.1: bytes=32 time<1ms TTL=128

Baffling....

Just remember what \r does. In your original code, if \r appeared at the end
of each line, the line would print, then \r would return the cursor to the
beginning of the same line, then --- would print, then \n would cause a
newline break.

Change your code to:

$ ping localhost | ruby -e '$<.readlines.each { |line|  line.chomp!();
p (line + "---\n");  }'

...and you'll be able to see the special characters escaped.

Sean O'Dell
 
A

Aredridel

I think I figured it out:
p line

" Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\r\n"

I don't know where that came from...

Wow. I'd be curious to know. Perhaps Ruby doing some misinterpretation?
 
E

Eric Hodel

--qse+WBH4guesipZ+
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello,
=20
I'm having problems with an one-liner (ruby 1.6.8 (2003-02-25)=20
[i386-cygwin]).
=20
My one liner sample:
=20
$ ping localhost | ruby -e '$<.readlines.each { |line| line.chomp!();=20
puts(line + "---\n"); }'
=20
I would expect it to put --- at the end of each line. Instead it=20
replaces the first 3 chars of each line with "---"
=20
What am I doing wrong? And why? :)

The real reason has been adequately addressed in the rest of this
thread.

If you want #chomp to remove the line-ending, you must chomp with $/,
which is the input record separator ($/ is cross-platform, so your
one-liner will work anywhere):

ruby -e "readlines.each {|l| puts %!#{l.chomp $/}!}"

Note that you don't have to use $< with -e, because readlines is a
Kernel method that reads from $< by default (I don't even know what $<
is).

Note that I used %!...! for the interpolated string to get around the
stupid windows shell. You can use just about any character after the %,
except for a few special ones ([qQsSwW] I think). (I typically use ! or
^.)

Note that I also used string interpolation, %!#{...}--!, rather than
storing to a temporary and using string concatenation. String
interpolation is both faster to type and faster to interpret.

To find out what all the special global variables are ($<, $:, $/, etc)
see English.rb in your ruby library directory.

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--qse+WBH4guesipZ+
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQFA5a9wMypVHHlsnwQRAuKdAKCRtoCqBp28IE3Za3kogr6DXeCDqgCbBMP8
y5q0okbLjrvu3TZKVjsrHew=
=LvtF
-----END PGP SIGNATURE-----

--qse+WBH4guesipZ+--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top