?
Solved

read no of lines

Posted on 2003-03-05
8
Medium Priority
?
199 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:maby
8 Comments
 

Expert Comment

by:sykkn
ID: 8073024
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
-----------------------------------------------------------
0
 
LVL 27

Accepted Solution

by:
wilcoxon earned 300 total points
ID: 8073032
I think this will do what you want.

# cut grabs only the job name (on Solaris - you might need to change the chars grabbed on other OS)
open PS, "ps -ef | cut -c 48-80 | grep ^$word |" or die "could not open ps pipe: $!\n";

my $cnt = 0;
my @jobs;
while (<PS>) {
    chomp;
    next unless /$some_regex/;  # this is probably unnecessary but would allow further checking
    push @jobs, $_;
}

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

Author Comment

by:maby
ID: 8079266
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 27

Expert Comment

by:wilcoxon
ID: 8081303
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.
0
 

Expert Comment

by:mrmoebius
ID: 8086766
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
       
0
 

Author Comment

by:maby
ID: 8101256
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
0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 8105016
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.
0
 

Author Comment

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

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question