We help IT Professionals succeed at work.

# Perl script to parse log and delete the file

on
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.

Comment
Watch Question

## View Solution Only

Most Valuable Expert 2014
Top Expert 2015

Commented:
$_='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;

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
Most Valuable Expert 2014
Top Expert 2015

Commented:
{local $/;$_=<>}
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1; 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;  The output: Can't find string terminator ' " ' anywhere before EOF at C:\Path\to\Perl_script.pl line 2.  Commented: Tried this #!/usr/bin/perl {local$/;$_=<>} /executing: \@(.*)(?ms:.*?ERROR at)/ && unlink$1;


Ran it as
Perl_script.pl log.txt

. 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
Most Valuable Expert 2014
Top Expert 2015

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


Still it does not delete the file and does not warn or error. :-(
Most Valuable Expert 2014
Top Expert 2015

Commented:
remove the # from #{local $/;$_=<>}

Commented:
Uncommented the above mentioned line ..still nothing. :-(
Most Valuable Expert 2014
Top Expert 2015

Commented:
#!/usr/bin/perl
{local $/;$_=<>};
if( /executing: \@(.*)(?ms:.*?ERROR at)/ ){
unlink $1 or warn "$1 $!"; }else{ warn "can't find /executing: \@(.*)(?ms:.*?ERROR at)/ in$_";
}


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 Most Valuable Expert 2014 Top Expert 2015 Commented: 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;

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. Commented: The idea here is to only delete files that errored out in the log. Commented: Executed : #!/usr/bin/perl {local$/;$_=<>} unlink$1 or warn "$1$!" while /executing: \@(.*)(?ms:.*?ERROR at)/g;


OUTPUT:

syntax error at C:\Perl_script.pl line 3, at EOF
Execution of C:\Perl_script.pl aborted due to compilation errors.
Most Valuable Expert 2014
Top Expert 2015

Commented:
can you check whether you left extra characters at the end of C:\Perl_script.pl ?

Commented:
Thanks ..It was extra { in the script.