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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing the number of rows in two files

Status
Not open for further replies.

Dudek

Technical User
Jan 14, 2002
27
MY
Hi there,

How can i compare the number of rows in two files (UNIX)?

Basically what i need to do is:

if (compare number of rows ('file1', 'file2')==0)
{
do something;
}

so is there any way to do the comparison of the two files to see whether they have the same number of rows?

TIA
JD
 
a simple way to do this would be to use the standard 'wc' utility

in your compare_number_of_rows() sub call 'wc -l' on each file, reading the results into a variable, like this:

my $f1_wc = `wc -l $file1`;
my $f2_wc = `wc -l $file2`;

then you can examine the two variables, $f1_wc and $f2_wc,and return either 1 (true) or 0 (false)
[tt]
use strict;
use warnings;

sub do_something(){
print "equal\n";
}

sub compare_number_of_rows($$){
my ($file1, $file2) = @_;

$_ = `wc -l $file1`;
my ($lc1, undef) = split();

$_ = `wc -l $file2`;
my ($lc2, undef) = split();

if($lc2 == $lc1){
return(1); # true
} else {
return(0); # false
}

}

if (compare_number_of_rows('file1', 'file2')){
do_something();
}
[/tt]

Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top