jasonwolf has asked for the wisdom of the Perl Monks concerning the following question:

I am currently working on a simple BASH script for a Nagios plug-in; however, BASH does not do floating point integer, so I wanted to use this time for Perl.

BASH code

poltx=$( snmpget -v 2c -c readonly 192.168.1.23 IF-MIB::ifInOctets.5001 | awk {'print $4'})

I am able to wrap cli commands in `command`, so I can wrap commands; however, I am a little lost at the awk portion. I have reviewed a number of forums; however, I am still lost, and wanted to communicate with someone to ask questions on how I can do this as easiest possible, so I can learn at the same time.

Thank you JW

Replies are listed 'Best First'.
Re: simple awk feature in perl
by Corion (Patriarch) on Feb 24, 2015 at 17:08 UTC

    There is a2p, which converts awk scripts to Perl code. In your case, it creates the following program:

    #!/opt/perl-5.18/bin/perl eval 'exec /opt/perl-5.18/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible) eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift; # process any FOO=bar switches $, = ' '; # set output field separator $\ = "\n"; # set output record separator while (<>) { ($Fld1,$Fld2,$Fld3,$Fld4) = split(' ', $_, -1); print $Fld4; }

    Most of that is just setting up explicitly what perl has as defaults anyway. The interesting part (for you) is the while loop. Either use that in a oneliner or keep the whole thing as a Perl program.

Re: simple awk feature in perl
by VinsWorldcom (Prior) on Feb 24, 2015 at 17:17 UTC

    I'm confused as to "floating point integer" - that seems an oxymoron. It's floating point or integer, no?

    In any case the ifInOctets is a Counter32 type which is an integer so I don't think the floating point issues comes up at all.

    Instead of "...| awk {'print $4'}", you could use:

    ...| perl -ane 'print $F[3]'

      Maybe "floating point integer" is in the format 1.2, in that case, play around with echo "1" | awk '{print sprintf("%0.1f", $1)}' (prints "1.0")

Re: simple awk feature in perl
by GotToBTru (Prior) on Feb 24, 2015 at 17:05 UTC

    awk is performing, by default, what in perl would be:

    ($1,$2,$3,$4,$5...) = split /\s+/,`snmpget ... `;
    Dum Spiro Spero
Re: simple awk feature in perl
by jasonwolf (Sexton) on Feb 24, 2015 at 19:08 UTC

    Thank you for the help. I was able to get beyond my issue with great success. I am not a programmer, and I am just learning scripting, so maybe I was confusing the two "floating point, and integer". Yet, I was able to cobble something together. thank you