Perl script to parse log and delete the file

Below is the log file I have. I need help to create a perl script which will

1. Look for the word ERROR at string in the log and
2.  Find the first occurrence of line executing: @  before the word ERROR at and
3. Delete the file following the @ sign
4. If the file does not exist while trying to delete dont error out.


In the below log file once the ERROR at string is found ........ONLY the file at C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql needs to be deleted.




connecting to Oracle with C:\oracle\instantclient/sqlplus.
executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql
CREATE OR REPLACE P PUBLIC SYNONYM da99_prt
                  *
ERROR at line 1:
ORA-00922: missing or invalid option 


Finished execution.

connecting to Oracle with C:\oracle\instantclient/sqlplus.
executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_88.sql

Finished execution.

Open in new window

gaurav sharmaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
$_='connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql
CREATE OR REPLACE P PUBLIC SYNONYM da99_prt
                  *
ERROR at line 1:
ORA-00922: missing or invalid option


Finished execution.

connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_88.sql

Finished execution.
';
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1;
0
gaurav sharmaAuthor Commented:
Thank you Ozo for the quick reply. I need to pass the log file as a parameter to the perl script.  Something like

Parse_and_delete_file.pl C:\path\to\logfile.txt
0
ozoCommented:
{local $/;$_=<>}
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1;
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

gaurav sharmaAuthor Commented:
Put the content in perl script as below and ran it

Perl_script.pl log.txt        (log.txt and the perl script are in same directory here)

#!/usr/bin/perl
$_=join'',<>;
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1; 

Open in new window


The output:

Can't find string terminator ' " ' anywhere before EOF at C:\Path\to\Perl_script.pl line 2.

Open in new window

0
gaurav sharmaAuthor Commented:
Tried this
#!/usr/bin/perl
{local $/;$_=<>}
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1;

Open in new window


Ran it as
Perl_script.pl log.txt

Open in new window

. In this case the script it did not error out but did not delete the file test_99.sql


Please let me know if you have further questions.

Thanks,
Gaurav
0
ozoCommented:
Does
/executing: \@(.*)(?ms:.*?ERROR at)/ && (unlink($1) or warn "$1 $!");
report anything?
0
gaurav sharmaAuthor Commented:
Ran the below script ..


#!/usr/bin/perl
#{local $/;$_=<>}
/executing: \@(.*)(?ms:.*?ERROR at)/ && (unlink($1) or warn "$1 $!");

Open in new window



Still it does not delete the file and does not warn or error. :-(
0
ozoCommented:
remove the # from #{local $/;$_=<>}
0
gaurav sharmaAuthor Commented:
Uncommented the above mentioned line ..still nothing. :-(
0
ozoCommented:
How about with
#!/usr/bin/perl
print "reading: @ARGV\n";
{local $/;$_=<>};
if( /executing: \@(.*)(?ms:.*?ERROR at)/ ){
  unlink $1 or warn "$1 $!";
}else{
  warn "can't find  /executing: \@(.*)(?ms:.*?ERROR at)/ in $_";
}

Open in new window

0
gaurav sharmaAuthor Commented:
The above script works for the very first instance of the ERROR at  that means if the log has two entries of ERROR at instances its only deleting the file mentioned in the first instance and not the second instance.

$_='connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql
CREATE OR REPLACE P PUBLIC SYNONYM da99_prt
                  *
ERROR at line 1:
ORA-00922: missing or invalid option


Finished execution.

connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_88.sql


ERROR at line 1:
ORA-00922: missing or invalid option


Finished execution.


============================

In the above case both the files test_99.sql and test_88.sql needs to be deleted ..not just test_99.sql.

Thanks
0
ozoCommented:
Sorry, I thought you said
ONLY the file at C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql needs to be deleted.
To delete them all, try
#!/usr/bin/perl
{local $/;$_=<>}
unlink $1 or warn "$1 $!" while /executing: \@(.*)(?ms:.*?ERROR at)/g;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gaurav sharmaAuthor Commented:
Sorry about the confusion. For example if I had the below log file ...test_77.sql should not be deleted as it executed successfully.(Since there was no error)

$_='connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_99.sql
CREATE OR REPLACE P PUBLIC SYNONYM da99_prt
                  *
ERROR at line 1:
ORA-00922: missing or invalid option


Finished execution.

connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\test_88.sql


ERROR at line 1:
ORA-00922: missing or invalid option


Finished execution.

connecting to Oracle with C:\oracle\instantclient/sqlplus.
    executing: @C:\Dir1\jobs\AVA\work\ora\arc\\test_77.sql


Finished execution.
0
gaurav sharmaAuthor Commented:
The idea here is to only delete files that errored out in the log.
0
gaurav sharmaAuthor Commented:
Executed :

#!/usr/bin/perl
{local $/;$_=<>}
unlink $1 or warn "$1 $!" while /executing: \@(.*)(?ms:.*?ERROR at)/g; 

Open in new window


OUTPUT:

syntax error at C:\Perl_script.pl line 3, at EOF
Execution of C:\Perl_script.pl aborted due to compilation errors.
0
ozoCommented:
can you check whether you left extra characters at the end of C:\Perl_script.pl ?
0
gaurav sharmaAuthor Commented:
Thanks ..It was extra { in the script.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.