Hi edison.pioneer,

Your immediate error:

print "RULE_FILE : $rulefile \n";
At this point in the code, the $rulefile has not been declared (which is done with the my built-in), leading to a compile-time error, and has not been defined or initialized (which is done by assigning a value to it), leading to a run-time error once the compile-time error has been fixed.

Next error (assuming you have added the my function for every new variable):

my $rulesdir = "C:\\Snort\\rules\\*.rules";
This does not define a directory, so that the next code line will not work as expected. If you want to define a directory, you may want to have:
my $rulesdir = "C:\\Snort\\rules\\";
or perhaps simply:
my $rulesdir = "C:/Snort/rules/";
Next issue:
@rulefiles = `ls $rulesdir\/*.rules`;
From looking at your directory paths, you're working on Windows. The ls command is a Unix system command, it will not work under Windows. You should use Perl internal commands rather than system calls whenever possible. Try this:
my $rulesdir = "C:\\Snort\\rules"; my @rulefiles = glob "$ruledir/*.rules";
Another issue (assuming that you have used my and indented your code correctly, as recommended, and straitening a bit the syntax):
for my $rulefile (@rulefiles){ open my $INFILE, "<", $rulefile or die "Can't open $rulefile $!"; my @rules = <$INFILE>; close $INFILE; }
Here, I have corrected and/or improved the syntax, but you have a serious algorithmic problem: you are looping over a list of files (@rulefiles) and, each time through the loop, are assigning the contents of the file to @rules, meaning that when you read the content of the second file, you clobber the previous content of @rules (i.e. what you read from the first file), and so on, so that, in fine, you end up with only the content of the last file.

There are several (actually, many) other issues, but at least, I hope this will help you going forward.


In reply to Re: Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling by Laurent_R
in thread Scanning multiple files from Snort rules one by one and extracting a particular part to another file - File Handling by edison.pioneer

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.