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

P

Peña, Botp

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
 
J

Jim Freeze

* "Peña said:
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
 
J

James Edward Gray II

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
 
J

Jason Sweat

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
 
W

WATANABE Hirofumi

Hi,

Jason Sweat said:
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
 
F

Florian Gross

WATANABE said:
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:
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top