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

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.
0
Mark Geerlings
Asked:
Mark Geerlings
  • 2
  • 2
1 Solution
 
ozoCommented:
egrep -L '\*\*\* (ACTION|MODULE) NAME' `grep -l 'ksudlio1: OER 2395' *` | xargs rm
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
OK, I copied two files (one that I wanted to delete, and one that I wanted to keep) to my /tmp folder, then tried that command there, and it seemed to work as intended.

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?
0
 
ozoCommented:
That's correct.
You can omit the | xargs rm to see what it would delete
0
 
Mark GeerlingsDatabase AdministratorAuthor Commented:
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.
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.

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