> Thomas Burkhardt <[EMAIL PROTECTED]> said: > Greetings Perl Gurus! > Using Perl, how do count the number of lines in a given file? > > I know how to run the code: > > open(MYPIPE, "|wc -l"); > print MYPIPE "apples\npears\npeaches\n"; > close MYPIPE; If you want to simply print the number of lines in a file, you can do it with the system() function. e.g. system("wc -l $filename"); If you want to count the lines without help from the "wc" program, it looks like: my $filename = "/etc/passwd"; my $count = 0; open(IN, "<$filename") || die "cannot open $filename - $!\n"; while (<IN>) { $count++; } close IN; print "$count\n"; Now if you want to open a file and shove it thru a filter it looks like: my $filename = "/etc/passwd"; open(IN, "<$filename") || die "cannot open $filename - $!\n"; open(PIPE, "| wc -l") || die "cannot open pipe - $!\n"; while (<IN>) { print PIPE; } close IN; close PIPE; Note you can do this by slurping the entire file into an array, but that uses up memory with large files. This version looks like: my $filename = "/etc/passwd"; open(IN, "<$filename") || die "cannot open $filename - $!\n"; open(PIPE, "| wc -l") || die "cannot open pipe - $!\n"; print PIPE <IN>; close IN; close PIPE; I prefer to read files line by line unless there is a specific reason to bring the entire file into memory. -- Smoot Carl-Mitchell Consultant -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]