[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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
0
anshuma
Asked:
anshuma
  • 3
  • 2
  • 2
2 Solutions
 
ozoCommented:
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
 
anshumaEngineeringAuthor Commented:
I didn't change the file name , I guess. But I need to pass the file name as an argument
0
Technology Partners: 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!

 
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
 
manav_mathurCommented:
#!/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

Featured Post

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.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now