Help with one-liner

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
---
---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? :slight_smile:

Thanks.

Best regards.
philip
----
break; /* don't do magic till later */
  -- Larry Wall in stab.c from the perl source code
---

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

···

On Sat, 3 Jul 2004 01:13:17 +0900, Philip Mateescu <pmateescu@novosadhayes.com> wrote:

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 "---"

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Philip Mateescu wrote:

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? :slight_smile:

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.

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

···

On Sat, 2004-07-03 at 01:13 +0900, Philip Mateescu wrote:

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? :slight_smile:

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.

···

Philip Mateescu (pmateescu@novosadhayes.com) wrote:

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 "---"

What am I doing wrong? And why? :slight_smile:

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

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 :slight_smile:

Austin Ziegler wrote:

···

On Sat, 3 Jul 2004 01:13:17 +0900, Philip Mateescu > <pmateescu@novosadhayes.com> wrote:

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

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

---

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....

Aredridel wrote:

···

On Sat, 2004-07-03 at 01:13 +0900, Philip Mateescu wrote:

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
---
---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? :slight_smile:

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
----
All I ask is a chance to prove that money can't make me happy.
---

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

martin

···

Austin Ziegler <halostatue@gmail.com> wrote:

#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.

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

Looks like the third one was close...

Baffling....

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

Ari

···

On Sat, 2004-07-03 at 01:41 +0900, Philip Mateescu wrote:

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

···

On Friday 02 July 2004 09:41, Philip Mateescu wrote:

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....

Aredridel wrote:

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

Looks like the third one was close...

... but no cigar :slight_smile:

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

···

On Sat, 2004-07-03 at 01:41 +0900, Philip Mateescu wrote:

----
Never assign to malice what can be adequately explained by stupidity...

---

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?

[Aredridel <aredridel@nbtsc.org>, 2004-07-02 19.17 CEST]

> 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?

No, redirecting to a file shows the extra \r on each line.