You are correct, a hash could be used to track number of occurrances of a given line in a file. I construct my hash from an initial array of all the lines you'd like to be present in the file and go from there.
use strict; # more lines could be added to this list my @lines = ("set CMP_DATA_INBND_DIR=C:\\h\\csscs\\data\\commi\\tmp_qu +eue", "set CMP_DATA_OUTBND_DIR=C:\\h\\CMP\\data\\outbound", "set JAVA_HOME=C:\\h\\COTS\\JAVA2\\1.3", "set CSSCS_DATA=C:\\h\\csscs\\data"); # construct a hash of initial line count # e.g. # ( # lineA => 0, # lineB => 0 # ... # ) my %lines = map { $_ => 0 } @lines; my $file = shift or die "Input file argument missing!\n"; my $outfile = shift; # determine default output file name if not specified by the user. $outfile ||= "$file.out"; open (IN, "<$file") or die "Failed to open file $file!\n"; open (OUT, ">$outfile") or die "Failed to open file $outfile!\n"; while (<IN>) { chomp; # increment line count if present in the file $lines{$_}++ if exists $lines{$_}; # for now just print back to the output file print OUT "$_\n"; } for (keys %lines) { # print lines that were missed from the input file # (count 0) print OUT $_ . "\n" unless $lines{$_}; } close(IN); close(OUT);
Sample input file:
set FOO=BAR set CMP_DATA_INBND_DIR=C:\h\csscs\data\commi\tmp_queue set BAR=FOO set CSSCS_DATA=C:\h\csscs\data
And output file generated by the script:
set FOO=BAR set CMP_DATA_INBND_DIR=C:\h\csscs\data\commi\tmp_queue set BAR=FOO set CSSCS_DATA=C:\h\csscs\data set CMP_DATA_OUTBND_DIR=C:\h\CMP\data\outbound set JAVA_HOME=C:\h\COTS\JAVA2\1.3


update: added some badly needed comments..

_____________________
# Under Construction

In reply to Re: Checking file for a set of strings by vladb
in thread Checking file for a set of strings by kirk123

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.