#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Find the latest directory and loop through the files and pick the error messages


    Hi,

    I am new to unix and shell scripting,can anybody help me in sctipting a requirement.

    my requirement is to get the latest directory the name of the directory will be like CSB.monthdate_time stamp
    like CSB.Sep29_11:16 and CSB.Oct01_16:21.
    i need to pick the latest directory.

    in the directory there will be a 5 error files like
    THerrFile_1.err
    THerrFile_2.err
    THerrFile_3.err
    THerrFile_4.err
    THerrFile_5.err.

    I need to go thorugh each one of the file and pick the ORA- error codes and ora message from the file.

    Please advice.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    The easiest way would be if you did not need to rely upon the name of the file but the last modification date which, for log files, should be pretty much the same thing.

    Code:
    ls -1trp CSB.* | grep "/$" | tail -1
    To list (ls) contents of directory entries starting with "CSB." (CSB.*) showing just the name, one per line (1) in reverse (r) date/time (t) order and add a / to the name if the entry is a directory (p). Pass thatm using the pipe command (|) to grep, looking for only entries that end ($) with the / (meaning it's a directory). Then pass that (again, with the pipe command) to tail and just select the last entry (-1) which will then be the latest.

    If, in a script, you allocate that name to a variable you could then use that to 'peek' inside the directory and look for (using the grep command) the strings you are interested in.

    Code:
    current_dir=$(pwd)
    latest_dir=$(ls -1trp CSB.* | grep "/$" | tail -1)
    
    cd $latest_dir
    grep "ORA-" THerrFile_?.err > ${current_dir}Latest_ORA-err.txt
    grep "ora" THerrFile_?.err > ${current_dir}Latest_oramsg.txt
    
    cd $current_dir
    There are other ways of doing it, some better, some worse.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0
    Hi,

    Thanks SimonJM.

    The problem is that there is a log file CSB_control.log this comes up as the latest file.

    if i am using ls -ltrd CSB*|tail -1
    so the latest directory is not comming.


    Please help.






    Originally Posted by SimonJM
    The easiest way would be if you did not need to rely upon the name of the file but the last modification date which, for log files, should be pretty much the same thing.

    Code:
    ls -1trp CSB.* | grep "/$" | tail -1
    To list (ls) contents of directory entries starting with "CSB." (CSB.*) showing just the name, one per line (1) in reverse (r) date/time (t) order and add a / to the name if the entry is a directory (p). Pass thatm using the pipe command (|) to grep, looking for only entries that end ($) with the / (meaning it's a directory). Then pass that (again, with the pipe command) to tail and just select the last entry (-1) which will then be the latest.

    If, in a script, you allocate that name to a variable you could then use that to 'peek' inside the directory and look for (using the grep command) the strings you are interested in.

    Code:
    current_dir=$(pwd)
    latest_dir=$(ls -1trp CSB.* | grep "/$" | tail -1)
    
    cd $latest_dir
    grep "ORA-" THerrFile_?.err > ${current_dir}Latest_ORA-err.txt
    grep "ora" THerrFile_?.err > ${current_dir}Latest_oramsg.txt
    
    cd $current_dir
    There are other ways of doing it, some better, some worse.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    It's not d it's p ... also its 1 not l (that's digit one, not letter ell)

    Code:
    ls -1trp CSB.* | grep "/$" | tail -1
    And don't forget the grep "/$" to pick out just directories.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    It is comming out when we donot find a file


    Hi,

    the code that i pasted in my previous posts works fine ,if all files are existing.if any file is missing it is comming out.

    but my requirement is,it should go to next file and grep the ora-messages.
    i am copying my code also.

    for example if we couldn't find THerrFile_1.err file in the directory.it is comming out,it should go to
    THerrFile_2.err ,THerrFile_3.err ,THerrFile_4.err ,THerrFile_5.err files and pick the ora-messages.


    please advice.

    Code:
    #catr.ksh
    #!/bin/ksh
    
    v=""
    a=$(ls -ltrd CSB.*|tail -1|egrep -v '^CSB.*'|awk '{ print$9 }')
    cd $a
    rm oraerr_output.txt
    for j in 1 2 3 4 5
    do
    v="THerrFile_$j.err";
    grep -i "$j:Error" $v >>oraerr_output$j.txt
    cat oraerr_output$j.txt >> oraerr_output.txt
    rm oraerr_output$j.txt
    done

IMN logo majestic logo threadwatch logo seochat tools logo