Hello Monks, I am a novice at Perl, but am enjoying learning the language thus far. However, I have run into an obstacle and am totally clueless how to proceed. This is my code:
#!/usr/bin/perl use strict; use Getopt::Long; #GetOpt::Long::Configure('bundling'); #sub process_args(); #sub pars0r(); &process_args; &open_sesame; &pars0r; #my $hostname; my $vol_to_parse; my $file_to_parse; my @raw_data; my $status; my $volume; sub process_args(){ $status = GetOptions ( 'v=s' => \$vol_to_parse, # volume to parse argumento 'h=s' => \$file_to_parse); print "Process arg subrountine real\n"; print "Using: "."vol: $vol_to_parse". " and " . "file: $file_to_parse" + . "\n"; } #$file_to_parse="netapp_volume_location"; sub open_sesame{ open(DAT, $file_to_parse) || die("Could not open file! Is it there?"); @raw_data=<DAT>; #Take everything from file and put in @raw_data array close(DAT); print "open and close ran \n"; } #my @arrayz=($raw_data[0]..$raw_data[-1]); print "Gonna print whole array"; print "$raw_data[0..-1]"; my $y = qw ($raw_data[0]..$raw_data[3]); sub pars0r{ # foreach $volume (@arrayz) foreach $volume (@raw_data) { chomp($volume); # print "$volume" . "\n"; if ($volume =~ $vol_to_parse) { print "$vol_to_parse"." ". "got got \n"; } # elsif ($volume !=~ $vol_to_parse) # { # redo; #} else{ print "Did not find volume!\n"; } } }
Pretty much I am using a Nagios plugin to check a Net-App filer and I export all volumes of the NetApp filer's diskusage to a file. I want to parse this file line by line and return each line if it matches a certain phrase (volume name ie. $vol_to_parse). At the same time, the plugin not only produces each volumes disk usage line by line into a text file for me, it also unfortunately reproduces every volume in a nasty mess at the end of the file. This is why I was trying to do: my @arrayz=($raw_data[0]..$raw_data-1);

however my output from my code of a file1.txt with the info contains on 4 seperate lines:

hello

goodbye

john

dillinger

and my output is:

root# perl ult_test.pl -v john -h file1.txt

Process arg subrountine real

Using: vol: john and file: file1.txt

open and close ran

Did not find volume!

Did not find volume!

john got got

Did not find volume!

Gonna print whole arraygoodby.

I only want it to return the line with john in it. Am I going about this all wrong? Any help would be greatly appreciated. Thank you.

Sluggo


In reply to Parsing a file line by line until a certain indice in array by sluggo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.