Mark Geerlings
asked on
Bash script to delete some log files based on content
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...
or
*** 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.
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
*** 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...
or
*** 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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's correct.
You can omit the | xargs rm to see what it would delete
You can omit the | xargs rm to see what it would delete
ASKER
Thanks for the help! It is very cryptic (for people like me with no UNIX experience, many things in Linux seem very cryptic) but it does exactly what I asked for.
ASKER
Before I accept your suggestion as the answer though can you help me understand it?
The second part of the command (the "grep...") inside the two ` ` marks must be executed first to find all files (based on the *, but I could put a partial file name with a wild card, like: ldb1*.trc there, right)? Then the first part of the command ("egrep...") must evaluate the results of the "grep" part, and exclude any files that contain the pattern "ACTION NAME" or MODULE NAME", then the last part of the command (| xargs rm) does the actual delete, right?