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

Discussion in 'Ruby' started by Peña, Botp, Mar 21, 2005.

  1. Peña, Botp

    Peña, Botp Guest

    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, Mar 21, 2005
    #1
    1. Advertising

  2. Peña, Botp

    Jim Freeze Guest

    * "Peña, Botp" <> [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
     
    Jim Freeze, Mar 21, 2005
    #2
    1. Advertising

  3. 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
     
    James Edward Gray II, Mar 21, 2005
    #3
  4. Peña, Botp

    Jason Sweat Guest

    On Mon, 21 Mar 2005 12:51:43 +0900, James Edward Gray II
    <> 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)'

    seemed to work for me

    Regards,
    Jason
    http://blog.casey-sweat.us/
     
    Jason Sweat, Mar 21, 2005
    #4
  5. > cat /var/log/squid/access.log | ruby -pe '$_.gsub!(/^(\d+)/,
    > Time.at($1.to_i).to_s)'


    No need for "cat" there.
     
    Michael Campbell, Mar 21, 2005
    #5
  6. Peña, Botp

    Jason Sweat Guest

    On Mon, 21 Mar 2005 14:04:10 +0900, Michael Campbell
    <> wrote:
    > > 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
     
    Jason Sweat, Mar 21, 2005
    #6
  7. Hi,

    Jason Sweat <> 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, Mar 21, 2005
    #7
  8. 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}'
     
    Florian Gross, Mar 21, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Corbin
    Replies:
    3
    Views:
    106
    Robert Klemme
    Oct 13, 2003
  2. zuzu
    Replies:
    7
    Views:
    250
    Aredridel
    Jul 19, 2004
  3. stevetuckner

    DRY ruby idiom

    stevetuckner, Sep 28, 2004, in forum: Ruby
    Replies:
    11
    Views:
    211
    Brian Candler
    Sep 29, 2004
  4. Corey
    Replies:
    18
    Views:
    242
    Dave Thomas
    Nov 12, 2004
  5. Giles Bowkett
    Replies:
    15
    Views:
    334
Loading...

Share This Page