Solved

Bash script to delete some log files based on content

Posted on 2007-11-30
4
2,786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
shell script help 1 156
Trying to install Node.js on Linux instance. Erroring.... 2 80
centOS 7 GUI is now showing. 20 1,751
Help with a backup script in Linux 4 76
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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