in reply to modifying script to ask for file name as input

Well, you came to the right place. ;-)

Perl is excellent at doing this kind of stuff. I have written the following script that does what you wanted. (I could write a perl one-liner-ish that does the job, but I felt a complete program is probably more useful to you as 1) a tutorial on file I/O, command-line parsing. 2) a fully working utility.)

#!/use/local/bin/perl -w use strict; use IO::File; use Getopt::Long; # Parse command line arguments and assign corresponding variables GetOptions ( 'i|input=s' => \( my $INPUT = undef ), 'o|output=s' => \( my $OUTPUT = "./data-in,./data-out" ), 'g|graph=s' => \( my $GRAPH = "./graph" ), ); unless ( defined $INPUT ) { print <<USAGE SNMP data parser Usage: $0 [option] Options: -i|--input [filename] SNMP router data file -o|--output [in,out] Optional. Default is "datain,dataout". -g|--graph [command] Optional. Default is "./graph" USAGE ; exit(1); } # get the names of the datain and dataout files # note that you can override these names from # the command-line my ($datain,$dataout) = split /,\s*/, $OUTPUT; my $f = new IO::File $INPUT, "r" or die "Could not open input file $INPUT"; my $di = new IO::File $datain, "w" or die "Could not create output file $datain"; my $do = new IO::File $dataout, "w" or die "Could not create output file $dataout"; while (<$f>) { # for each line in the input file next if /^\s*$/; # ignore empty lines my @col = split /,\s+/; # split on , followed by spaces my ($date) = $col[0] =~ /^"(.*)\s.*"$/; # want date only print $di "$date, $col[2]\n"; print $do "$date, $col[3]\n"; } undef $di; # force close of files undef $do; undef $f; # do the graph system($GRAPH) or die "Could not execute $GRAPH"; exit(0); __END__ example: ./script.pl -i snmp.log
And the output from sample data are -
data-in ------- 23/10/2003, 1.86 23/10/2003, 2.26 23/10/2003, 0.74 23/10/2003, 1.15 23/10/2003, 1.61 23/10/2003, 1.55 23/10/2003, 1.11 data-out -------- 23/10/2003, 1.91 23/10/2003, 1.53 23/10/2003, 0.46 23/10/2003, 1.53 23/10/2003, 0.72 23/10/2003, 1.08 23/10/2003, 0.52

Replies are listed 'Best First'.
Re: Re: modifying script to ask for file name as input
by Anonymous Monk on Dec 05, 2003 at 11:54 UTC
    Hi Guys,
    Thank you very much for the inputs.
    To Roger:
    The script seems to be doing everything apart from when the ./graph script wants to then graph the data in/out it will error.
    I had this problem that was why I piped the snmp data file to the commands | sed 's/"/ /g' | sed 's/,/ /g' to remove the ' " '
    The problem is it has to be formatted by removing the ' " '
    "23/10/2003 0.85,
    "23/10/2003 0.85,
    I tried to edit the script my self to no avail.Can you modify the script to do this.
    Thanks for the wonderful tutorial.
    Can you also put comments on your script so i can learn and use it to write other similar scripts.
    Thanks reagards,
    gab
      Can you post a fragment of the snmp log file here? So we can have a look at its actual format.

      I have modified my script to remove " and , characters to my best guess since I have not seen the actual data file. And I have added more comments in the code.

        Hi Roger and VSarkiss,

        here is a fragment of the raw snmp log file. The graph script and some other programs i use dont like '"' and ','.

        "23/10/2003 00:04:20", 302, 1.86, 1.91, 2411.04, 1189.08, 1221.97, 32.0
        "23/10/2003 00:09:18", 298, 2.26, 1.53, 2427.33, 1449.25, 978.08, 32.0
        "23/10/2003 00:14:30", 312, 0.74, 0.46, 767.43, 470.52, 296.91, 32.0
        "23/10/2003 00:19:16", 286, 1.15, 1.53, 1718.47, 738.94, 979.53, 34.0
        "23/10/2003 00:24:31", 315, 1.61, 0.72, 1491.54, 1031.62, 459.92, 32.0
        "23/10/2003 00:29:28", 297, 1.55, 1.08, 1681.56, 989.15, 692.4, 32.0
        "23/10/2003 00:34:25", 297, 1.11, 0.52, 1047.92, 712.31, 335.6, 32.0

        Roger I tried to run your modified script and it errored out as

        Global symbol "$str" requires explicit package name at ./script.pl line 45
        Global symbol "$str" requires explicit package name at ./script.pl line 46
        Global symbol "$str" requires explicit package name at ./script.pl line 47

        Vsarkiss your script worked brilliantly only problem is with the format of removing the '"' and ',' before sending it to the ./graph script

        Thank you guys I greatly appreciate your time and await your replies.
        Regards,
        Seun