Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Checking file for a set of strings

by kirk123 (Beadle)
on May 02, 2003 at 00:02 UTC ( [id://254854]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to find out if a file contain the following strings:
set CMP_DATA_INBND_DIR=C:\h\csscs\data\commi\tmp_queue 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
and if not to add them to the file. So if the first three strings exist but not the fourth then add the fourth string. I was thinking that this might need a hash. What do you thinks? --thanks

Replies are listed 'Best First'.
Re: Checking file for a set of strings
by vladb (Vicar) on May 02, 2003 at 00:29 UTC
    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
Re: Checking file for a set of strings
by converter (Priest) on May 02, 2003 at 23:20 UTC

    If you want to do this from the command line, you can pair the code down a little bit with in-place editing (borrowing vladb's sample data for comparison):

    #!/usr/bin/perl use warnings; use strict; # note: define the required strings with # the input record separator, so no chomp() # required for comparison my @required = ( "set CMP_DATA_INBND_DIR=C:\\h\\csscs\\data\\commi\\tmp_queue$/", "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$/", ); my %found; @found{@required} = (0) x @required; # setting $^I defines the filename "extension" # to be appended to the backup copy of the # original file and enables in-place editing: $^I = '.bak'; while (<>) { print; $found{$_}++ if defined $found{$_}; if (eof) { for (@required) { next if $found{$_}; print; } } }

    $ cat foo.txt 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 $ ./foo.pl foo.txt $ cat foo.txt 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://254854]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found