Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

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
SOLUTION
Avatar of ozo
ozo
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 Amit

ASKER

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

Avatar of Amit

ASKER

I didn't change the file name , I guess. But I need to pass the file name as an argument
#!/usr/bin/perl      
my $found;
while( <> ){
  if( /\bhello\b/i || /\bcould not find\b/i ){
      print;
      $found++;
  }
}
system("web.bat") unless $found;
Avatar of manav_mathur
manav_mathur

#!/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;
Avatar of Amit

ASKER

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

ASKER CERTIFIED SOLUTION
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