Solved

Bash script to delete some log files based on content

Posted on 2007-11-30
4
2,780 Views
Last Modified: 2010-04-21
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
Comment
Question by:Mark Geerlings
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 20384671
egrep -L '\*\*\* (ACTION|MODULE) NAME' `grep -l 'ksudlio1: OER 2395' *` | xargs rm
0
 
LVL 34

Author Comment

by:Mark Geerlings
ID: 20385592
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
 
LVL 84

Expert Comment

by:ozo
ID: 20385748
That's correct.
You can omit the | xargs rm to see what it would delete
0
 
LVL 34

Author Closing Comment

by:Mark Geerlings
ID: 31411973
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now