Executing awk from perl script

Discussion in 'Perl Misc' started by AyOut, Mar 24, 2007.

  1. AyOut

    AyOut Guest

    I'm new to perl and have the following line that I try to execute:

    ....
    my $hm = `date +%H:%M --date "1 minute ago"`;
    my $fname = `date +AppName_stats.log.%Y-%m-%d --date "1 days ago"`;

    my $hits = `find /logs/server_name0?/dir/. -name "$fname*" -exec
    gunzip -c {} ;\ | /grep SearchStr | /bin/awk -F","
    '{if(substr($1,12,5)~hmpoint)print $3}' hmpoint=$hm | /bin/awk '{tot+=
    $1}END{print tot}'`;

    When I run this I get all kinds of complaints on the $hits line. I've
    tried various modifications, but can't seem to get it to work. Any
    idea of what's wrong here?

    Thanks.
     
    AyOut, Mar 24, 2007
    #1
    1. Advertising

  2. AyOut wrote:
    > I'm new to perl and have the following line that I try to execute:
    >
    > ...
    > my $hm = `date +%H:%M --date "1 minute ago"`;
    > my $fname = `date +AppName_stats.log.%Y-%m-%d --date "1 days ago"`;
    >
    > my $hits = `find /logs/server_name0?/dir/. -name "$fname*" -exec
    > gunzip -c {} ;\ | /grep SearchStr | /bin/awk -F","
    > '{if(substr($1,12,5)~hmpoint)print $3}' hmpoint=$hm | /bin/awk '{tot+=
    > $1}END{print tot}'`;
    >
    > When I run this I get all kinds of complaints on the $hits line. I've
    > tried various modifications, but can't seem to get it to work. Any
    > idea of what's wrong here?


    In Perl you would write that as:

    use POSIX 'strftime';
    use File::Find;
    use Compress::Zlib;

    my $hm = strftime '%H:%M', localtime $^T - 60;
    my $fname = strftime 'AppName_stats.log.%Y-%m-%d', localtime $^T - 86400;

    my $hits;
    find sub {
    return unless /\A\Q$fname/;

    my $gz = gzopen( $_, 'rb' ) or die "Cannot open $_: $gzerrno\n";

    while ( $gz->gzreadline( my $line ) > 0 ) {

    if ( $line =~ /SearchStr/ && substr( $line, 11, 5 ) eq $hm ) {
    $hits += ( split /,/, $line )[ 2 ];
    }
    }

    die "Error reading from $_: $gzerrno\n" if $gzerrno != Z_STREAM_END;
    $gz->gzclose();

    }, glob '/logs/server_name0?/dir';


    > Thanks.


    You're welcome.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Mar 24, 2007
    #2
    1. Advertising

  3. AyOut

    Tintin Guest

    AyOut wrote:
    > I'm new to perl and have the following line that I try to execute:
    >
    > ....
    > my $hm = `date +%H:%M --date "1 minute ago"`;
    > my $fname = `date +AppName_stats.log.%Y-%m-%d --date "1 days ago"`;
    >
    > my $hits = `find /logs/server_name0?/dir/. -name "$fname*" -exec
    > gunzip -c {} ;\ | /grep SearchStr | /bin/awk -F","
    > '{if(substr($1,12,5)~hmpoint)print $3}' hmpoint=$hm | /bin/awk '{tot+=
    > $1}END{print tot}'`;
    >
    > When I run this I get all kinds of complaints on the $hits line. I've
    > tried various modifications, but can't seem to get it to work. Any
    > idea of what's wrong here?



    Why bother writing in Perl, if you are essentially writing a shell script?

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Tintin, Mar 25, 2007
    #3
  4. AyOut

    AyOut Guest

    On Mar 25, 5:24 am, Tintin <> wrote:
    > AyOut wrote:
    > > I'm new to perl and have the following line that I try to execute:

    >
    > > ....
    > > my $hm = `date +%H:%M --date "1 minute ago"`;
    > > my $fname = `date +AppName_stats.log.%Y-%m-%d --date "1 days ago"`;

    >
    > > my $hits = `find /logs/server_name0?/dir/. -name "$fname*" -exec
    > > gunzip -c {} ;\ | /grep SearchStr | /bin/awk -F","
    > > '{if(substr($1,12,5)~hmpoint)print $3}' hmpoint=$hm | /bin/awk '{tot+=
    > > $1}END{print tot}'`;

    >
    > > When I run this I get all kinds of complaints on the $hits line. I've
    > > tried various modifications, but can't seem to get it to work. Any
    > > idea of what's wrong here?

    >
    > Why bother writing in Perl, if you are essentially writing a shell script?
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    I'm writing the data to rrdtool.
     
    AyOut, Mar 25, 2007
    #4
  5. On 2007-03-25 14:47, Michele Dondi <> wrote:
    > On 25 Mar 2007 06:15:21 -0700, "AyOut" <> wrote:
    >>> Why bother writing in Perl, if you are essentially writing a shell script?

    >
    >>I'm writing the data to rrdtool.

    >
    > So what? Do you know that it comes with an accompanying Perl module?


    I think, that, yes, he knows that, since he answered the question "Why
    bother writing in Perl?" with "I'm writing the data to rrdtool". Maybe
    he doesn't know that he doesn't have to use Perl for that?

    hp


    --
    _ | Peter J. Holzer | Blaming Perl for the inability of programmers
    |_|_) | Sysadmin WSR | to write clearly is like blaming English for
    | | | | the circumlocutions of bureaucrats.
    __/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
     
    Peter J. Holzer, Mar 25, 2007
    #5
  6. AyOut

    Ian Wilson Guest

    Abigail Bush wrote:
    >
    > But I give you one hint: `` is a double quoted context. So Perl will
    > interpretate variables.
    >


    I interpret interpretate as interpolate.
     
    Ian Wilson, Mar 26, 2007
    #6
  7. AyOut

    Greg Bacon Guest

    In article <4607aaeb$0$28971$>,
    Ian Wilson <> wrote:

    : Abigail Bush wrote:
    :
    : > But I give you one hint: `` is a double quoted context. So Perl will
    : > interpretate variables.
    :
    : I interpret interpretate as interpolate.

    Conversate for a few
    'Cuz in a few
    We gon' do
    ....

    Sorry, don't mind me.

    Greg, hands in the air
    --
    A democracy is nothing more than mob rule, where fifty-one percent of
    the people may take away the rights of the other forty-nine.
    -- Thomas Jefferson
     
    Greg Bacon, Mar 26, 2007
    #7
    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. engineer

    Tool for perl script to awk

    engineer, Jun 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    918
    Joe Smith
    Jun 14, 2004
  2. Perl script to replace awk

    , Jul 26, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    148
    Geoffroy Braem
    Jul 27, 2004
  3. Replies:
    5
    Views:
    787
    John W. Krahn
    Apr 2, 2007
  4. Replies:
    3
    Views:
    204
    Josef Moellers
    Nov 1, 2007
  5. Generic Usenet Account

    Perl equivalent of simple awk script

    Generic Usenet Account, Nov 12, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    191
Loading...

Share This Page