Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2007
    Posts
    9

    Unanswered: 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.......

  2. #2
    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.

  3. #3
    Join Date
    May 2005
    Location
    South Africa
    Posts
    1,365
    Provided Answers: 1
    use
    grep -v
    to find files not containing the string

  4. #4
    Join Date
    Oct 2007
    Posts
    9
    Thank you friends, for your responses..all the commands work!!.........

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •