Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matching the contents of files? 2

Status
Not open for further replies.

rickict

IS-IT--Management
Apr 29, 2005
18
GB
hi i have a program that outputs the the installed programs on a computer to a .txt file in the following format.
filename = station3.txt

Adobe Acrobat 5.0
Ahead Nero OEM
AVG Anti-Virus 7.0
Bounce
Clicker 4
ControlIt
Counter for Windows
DataLogger
Dazzle
ETC...

i have over a hundred of these files in a folder from each computer that i manage. ive been trying to write a perl
script that compares all these files to a master file of the same format and inputs to a another file with each application name and how many computers it is installed on.

im new to perl and know the basics but im having trouble getting my head round this and was woundering if anyone had
anything simular to this that i can adapt to my needs.
i won't show you any of my attemps as they are quite pathetic..!

anyhelp will be very appreciated......thanks
 
Hi,

Don't know if this will be any help at all, but I use an Access database as a backend to my Perl. Large amounts of data like this are simpler to manipulate and examine in a database like Access. I can give you some code that will show you how to do this, if you like. If you just want to go the Perl route there are numerous examples of how to deal with this type of problem in the Perl Cookbook by Christiansen and Torkington.
 
thanks ive never used Access but im sure its a better solution.
any help would be great.
thanks for the tip on the book ill get that too.
 
If I was using Access the first thing I would do is to loop through all of your text files and collect the data into one file each record of which would perhaps look something like this:

station3, Adobe Acrobat 5.0
station 3, Ahead Nero OEM
station 4, Whatever
Etc..
Make sure that there is a unique delimiter like a comma, separating each field. Next import this file into an Access table using the The Get External Data -> Import... command from the File Section of the menu.
 
The access method would be fine if all of your client computers could write their installed programs list directly to the database but that might not be the case.

If you've got all the files in the same directory, this code loops over them and merely indexes the strings into a hash.
Code:
#!/usr/bin/perl

my %count;
foreach my $filename (<*.txt>) {
    open( FH, $filename ) or die "$0: $filename: $!";
    while(<FH>) {
        chomp();
        ++$count{$_} if $_; # ignore blank lines
    }
    close( FH );
}

open( FH, ">count.out" ) or die "$0: count.out: $!";
foreach my $app ( sort keys %count ) {
    print "$app - $count{$app}\n";
}
close( FH );

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
thanks Maurice
that does what i needed to do perfectly.
i popped in a couple of comma's and got it to output to a
.csv file .

perfect..
thanks again.
 
just for the record, sh/ksh/bash 1 liner:
sort *txt|uniq -c >count.out
 
Nice one-liner Arn0ld - for a UNIX box :)

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
On the subject of one-liners:
Code:
perl -ne 'chomp;$count{lc $_}++;END{print "$_ - $count{$_}\n" for( sort keys %count)}' *.txt
Bit longer than arn0ld's though!
 
re: UNIX box: It has been my impression that if PERL runs on a box, *nix utilities are probably available.

I checked the Marquis of Queensbury rules on 1 liners.
Use of ";" in place of new-line is considered below the coding belt.
 
Marquis of Queensbury-compliant version!
Code:
perl -ne '{chomp && $count{lc $_}++}END{print "$_ - $count{$_}\n" for( sort keys %count)}' *.txt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top