Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > deleting files conditionally

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-07, 14:48
sonali007 sonali007 is offline
Registered User
 
Join Date: Oct 2007
Posts: 9
deleting files conditionally

Hi all,

I am a newbie and learning the ropes....today i came across this forum and hope that i will find some help here......i have a script which runs every 5 mins in autosys...it will create log files irrespective of whether any event is found to process or not....this has created huge log files...i want to delete all those logs which do not have any event to process....the code should be given conditionally so that it only removes the files not needed.....for example:

files to retain will have information like:

event type.. abcd
<date and time stamp> .. connection successful..
event id : 1234
log file <path>

.......
.......
.......
pl/sql procedure successful....

those log files to be removed may look like:

event type..abcd
<date and time stamp>..connection successful...
event id :

and also

event type..abcd
<date and time stamp>..connection successful...
event id :
log file<path>
begin pkg........

error at line 1.
.......
......

i tried giving the code as

find <path_name> -exec grep -l 'pl/sql procedure successful' {} \; |xargs rm

but this will remove the file which has that grep string........the file which i want to retain.......

any immediate help is highly appreciated........
Thanks in advance.......
Reply With Quote
  #2 (permalink)  
Old 10-09-07, 18:04
radoulov radoulov is offline
Registered User
 
Join Date: May 2007
Location: Milano, Italy
Posts: 22
If you have GNU grep:

Code:
rm "$(grep -L 'pl/sql procedure successful' *)"

Otherwise you can pipe something like this to rm
(assuming the filenames contain no embedded spaces
or other pathological characters):
Code:
awk '{c[FILENAME]} /pl\/sql procedure successful/{c[FILENAME]++}END{ for(f in c)if(c[f]==0)print f}' *|xargs rm

Or write a shell loop:

Code:
for f in *;do grep -q 'pl/sql procedure successful' "$f"|| rm "$f";done

P.S. If your grep doesn't support the q option, use

Code:
grep 'pl/sql procedure successful' "$f">/dev/null 2>&1

Last edited by radoulov : 10-09-07 at 18:25.
Reply With Quote
  #3 (permalink)  
Old 10-10-07, 02:51
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 830
use
grep -v
to find files not containing the string
Reply With Quote
  #4 (permalink)  
Old 10-10-07, 12:16
sonali007 sonali007 is offline
Registered User
 
Join Date: Oct 2007
Posts: 9
Thank you friends, for your responses..all the commands work!!.........
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On