find a word and then execute a batch file

Hi I want to create a perl script that will look for a word "hello" or sentence "could not find" and if these two things are not found in the file then execute a dos batch file "web.bat".

However if found then it will write the entire line where it found the word "hello" in a file helloout.txt. Please help, I am a newbie to perl and my job requires creation of scripts for automated testing
anshumaEngineeringAsked:
Who is Participating?
 
manav_mathurConnect With a Mentor Commented:
#!/usr/bin/perl    
my $found;
open O,"> helloout.txt" or die "helloout.txt $!";
while( <> ){
  if( /\bjava\.lang\.exception\b/i || /\bcould not find\b/i ){
      print O $_ if (/\bjava\.lang\.exception\b/i) ;
      $found++;
  }
}
close(O) ;
system("web.bat") unless $found;
0
 
ozoConnect With a Mentor Commented:
open F,"<file" or die "Can't open file $!";
open O,">>helloout.txt" or die "helloout.txt $!";
my $found;
while( <F> ){
  if( /\bhello\b/i || /\bcould not find\b/i ){
      print O;
      $found++;
  }
}
close O;
system("web.bat") unless $found;
0
 
anshumaEngineeringAuthor Commented:
I saved it as test.pl in a directory testfolder

Then I went to that directory ans did

perl test.pl testfile.html

and this is the output I get  "Can't open file No such file or directory at test.pl line 1"

something wrong with the code you sent

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
anshumaEngineeringAuthor Commented:
I didn't change the file name , I guess. But I need to pass the file name as an argument
0
 
ozoCommented:
#!/usr/bin/perl      
my $found;
while( <> ){
  if( /\bhello\b/i || /\bcould not find\b/i ){
      print;
      $found++;
  }
}
system("web.bat") unless $found;
0
 
manav_mathurCommented:
#!/usr/bin/perl    
my $found;
open O,">>helloout.txt" or die "helloout.txt $!";
while( <> ){
  if( /\bhello\b/i || /\bcould not find\b/i ){
      print O $_ if /\bhello\b/i ;
      $found++;
  }
}
close(O) ;
system("web.bat") unless $found;
0
 
anshumaEngineeringAuthor Commented:
It currently appends to the helloout file if the file is existing can we modify it to write again instead of appending whenever the program is run. One last thing can we modify it for finding the word  java.lang.exception

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.