• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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

0
gaurav sharma
Asked:
gaurav sharma
  • 10
  • 7
1 Solution
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now