We help IT Professionals succeed at work.

Perl script to parse log and delete the file

gaurav sharma
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.

Open in new window

Comment
Watch Question

ozo
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;

Author

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

Commented:
{local $/;$_=<>}
/executing: \@(.*)(?ms:.*?ERROR at)/ && unlink $1;

Author

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

Author

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

Commented:
Does
/executing: \@(.*)(?ms:.*?ERROR at)/ && (unlink($1) or warn "$1 $!");
report anything?

Author

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

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

Author

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

Commented:
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

Author

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;

Author

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.

Author

Commented:
The idea here is to only delete files that errored out in the log.

Author

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

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

Author

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