?
Solved

read no of lines

Posted on 2003-03-05
8
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 26

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

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 26

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

764 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