Link to home
Start Free TrialLog in
Avatar of maby
maby

asked on

read no of lines

Hello,
my boss have assigned me to a hopeless task since I know pretty much nothing about Perl. So my hope stands to this forum. What I want to do is check if there is three jobs running, and I thought this might be a good start ?

cd /myDirectory/
rm ResultFile.txt
ps -ef > ResultFile.txt

Is there a way to check how many rows there are in the file ? Thay all start with the same word if that is any help ?

Please help.
Avatar of sykkn
sykkn

Here is the way that I would count the number of lines starting with 'word' ...

-----------------------------------------------------------
$i=0;
open(FILE,"/path/to/file") || die($!);
  while(<FILE>) {
    $i++ if m/^word/;
  }
close(FILE) || warn($!);

print "word appeared ",$i," times\n";
-----------------------------------------------------------

now if you just want the number of lines in a file you could just say ...

-----------------------------------------------------------
$myVar = `wc -l myexport`;  # get the output of wc
$myVar =~ /\s+(\d+)/;       # find just the count
$lineCount = $1;            # assign the account to a var
print $lineCount,"\n";      # print the count
-----------------------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maby

ASKER

Hi wilcoxon, that is exactly what I want to do ! Many thanks for your help, there is only one problem, I get
"syntax error: 'open' unexpected" all the time.

Am I doing some emaressingly stupid misstake ?

Regards
How exactly are you running the program?  You shouldn't be getting a syntax error if it's run through perl.  You should either have #!/path_to_perl/perl as the first line in your program (on Unix) or run it via a shell script, bat file, or manually (perl script.pl).

What happens if you type "perl -cw script.pl"?  Do you still get the syntax error?  If so, go ahead and post your script exactly as it is and I'll take a look at it.
Here's how I would approach it:

open FILE,"<ResultFile.txt";
  @file_lines = <FILE>;
close FILE;    
$number_of_lines = $#file_lines;

Does that help any?

Brian
       
Avatar of maby

ASKER

Hi again,
I've tried "perl -cw script.pl" with syntax OK as result. But I still get "open unexpected", and I run the script is as follows:
#! /usr/bin/sh

open (PS, "ps -ef | cut -c 20-80 | grep tomcat") or die "could not open ps pipe: $!\n";

my $cnt = 0;
my @jobs;
while (<PS>) {
   chomp;
   push @jobs, $_;
}

print "all jobs running\n" if (@jobs == 3);
print "only these jobs running: ", join(', ', @jobs), "\n";

Thanks for all help
Ah.  The problem is the very first line.

#!/usr/bin/sh

should be:

#!/path_to_perl/perl

/path_to_perl/perl will usually be /usr/bin/perl or /usr/local/bin/perl.
Avatar of maby

ASKER

Thanka again, it is now working !
I'm just gonna figure out how to distribute the points :)