Equivalent idiom for ruby " perl -pe 's/(\d+)/localtime($1)/e'"

Hi,

forgive my perl ignorance, but how do you convert the ff perl idiom to
ruby(way):

perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

thank you
-botp

* "Peña, Botp" <botp@delmonte-phil.com> [2005-03-21 12:14:31 +0900]:

Hi,

forgive my perl ignorance, but how do you convert the ff perl idiom to
ruby(way):

perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

I think it would be

  ruby -pe '$_.gsub!(/(\d+)/, Time.local("\\1".to_i).to_s)' /var..access.log

···

--
Jim Freeze
Code Red. Code Ruby

ruby -pe 'gsub!(/\d+/) { |t| Time.at(t.to_i) }' /var/log/squid/access.log

I think.

James Edward Gray II

···

On Mar 20, 2005, at 9:14 PM, Peña, Botp wrote:

Hi,

forgive my perl ignorance, but how do you convert the ff perl idiom to
ruby(way):

perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
Time.at($1.to_i).to_s)'

seemed to work for me

Regards,
Jason
http://blog.casey-sweat.us/

···

On Mon, 21 Mar 2005 12:51:43 +0900, James Edward Gray II <james@grayproductions.net> wrote:

On Mar 20, 2005, at 9:14 PM, Peña, Botp wrote:

> Hi,
>
> forgive my perl ignorance, but how do you convert the ff perl idiom to
> ruby(way):
>
> perl -pe 's/(\d+)/localtime($1)/e' /var/log/squid/access.log

ruby -pe 'gsub!(/\d+/) { |t| Time.at(t.to_i) }'
/var/log/squid/access.log

I think.

James Edward Gray II

cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
Time.at($1.to_i).to_s)'

No need for "cat" there.

Yes, I started from what Jim Freeze posted above, and switched to the
"cat" style during testing of the Time stuff, never switched it back.
I see that

ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)' /var/log/squid/access.log

works as well.

Regards,
Jason

···

On Mon, 21 Mar 2005 14:04:10 +0900, Michael Campbell <michael.campbell@gmail.com> wrote:

> cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
> Time.at($1.to_i).to_s)'

No need for "cat" there.

Hi,

Jason Sweat <jason.sweat@gmail.com> writes:

I see that

ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)' /var/log/squid/access.log

works as well.

$1 becomes a result of the match last time, beause $1 is
evaluated before gsub! is called.

% ruby -e '1111111111.upto(1111111115){|i| puts i}' |\
  ruby -pe '$_.gsub!(/^(\d+)/, Time.at($1.to_i).to_s)'
Thu Jan 01 09:00:00 JST 1970
Fri Mar 18 10:58:31 JST 2005
Fri Mar 18 10:58:32 JST 2005
Fri Mar 18 10:58:33 JST 2005
Fri Mar 18 10:58:34 JST 2005

So you should use a block.

% ruby -e '1111111111.upto(1111111115){|i| puts i}' |\
  ruby -pe '$_.gsub!(/^(\d+)/){Time.at($1.to_i).to_s}'
Fri Mar 18 10:58:31 JST 2005
Fri Mar 18 10:58:32 JST 2005
Fri Mar 18 10:58:33 JST 2005
Fri Mar 18 10:58:34 JST 2005
Fri Mar 18 10:58:35 JST 2005

Golf:

% echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at($&.to_i)}'
Fri Mar 18 10:58:31 JST 2005

···

--
eban

WATANABE Hirofumi wrote:

Golf:

% echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at($&.to_i)}'
Fri Mar 18 10:58:31 JST 2005

Hard to do much here, but here's two characters:

···

% echo 1111111111 |ruby -pe 'sub(/^\d+/){Time.at$_.to_i}'