Bash script to delete some log files based on content
Posted on 2007-11-30
We had an automated program with a problem that generated lots of log files from our Oracle database for some weeks. I would like to delete the log files that have a particular pattern of data in them, but keep the few other log files in the directory that have a similar, but slightly different pattern of data.
The files I would like to delete include these three lines:
*** SERVICE NAME:(SYS$USERS) 2007-11-30 08:40:00.409
*** SESSION ID:(1160.46) 2007-11-30 08:40:00.409
ksudlio1: OER 2395: ksuplres = 13880, ksupcio = 13881
The files I would like to keep include these five lines (the last three are the same as in the files I would like to delete):
*** ACTION NAME:(Thu 074551 ) 2007-11-29 08:04:46.381
*** MODULE NAME:(qa_quality_assurance.fmx) 2007-11-29 08:04:46.381
*** SERVICE NAME:(SYS$USERS) 2007-11-29 08:04:46.381
*** SESSION ID:(1155.17795) 2007-11-29 08:04:46.381
ksudlio1: OER 2395: ksuplres = 9c40, ksupcio = 9c41
So, if the file includes a line like:
*** ACTION NAME...
*** MODULE NAME
I would like to keep it, but I want to delete all files that include the line:
ksudlio1: OER 2395
if they do *NOT* include "*** ACTION NAME" or "*** MODULE NAME"
Is this possible in a BASH shell script? I don't mind using a two-step process, if that is easier, something like:
1. first find and rename all of the files I want to keep (those that include:
"*** ACTION NAME" or "*** MODULE NAME")
2. then delete the remaining files that include: "ksudlio1: OER 2395"
These files all have names like: "ldb1_xxxxx.trc" where "xxxxx" is a number to make them unique.
If these were records in an Oracle database, I could easily write the SQL statements to do this, but I am not a master of writing BASH shell scripts.