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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the latest directory and loop through the files and pick the error messages
# 1  
Old 10-04-2010
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  
Old 10-04-2010
Moderator's Comments:
Mod Comment This thread is not connected to the AIX operating system. I am going to transfer it to the "Shell Programming and Scripting" board therefore.


Quote:
Originally Posted by sudhir_83k
Hi,

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

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

Please advice.
Actually these are three distinct problems:

1) Find the latest directory and get its name;
2) cycle through all the files of a directory with a given name.
3) pick the ORA-messages

2) and 3) are rather easy to accomplish as long as there is no subdirectory structure possible (like in your requirement). You can use the "ls"-command to cycle through all directory entries and pipe the output of it into a while-loop. There you can use "grep" on these files:

Code:
#!/bin/ksh

# uncomment the following line to watch the script run:
# set -xv

typeset fFileName=""
typeset fDirectory="/path/to/your/directory"

ls ${fDirectory} | while read fFileName ; do
     # /path/to/yourcommand "${fFileName}" will cycle through the files
     print - "--------------------[ ${fFileName} ]--------------------------"
     grep "^ORA" ${fFileName}
done

Run this little script and try to understand how it works. Concepts to grasp are: (1) pipelines and how they work; (2) variable expansion; (3) the "while...read"-loop in ksh.

Your first problem - finding the latest directory - is a little more tricky, depending on your situation: every directory entry (that includes directories) has a time stamp and you can tell the "ls"-command to sort the entries by that time stamp (instead of alphabetically, which is the default). It might be that you are looking for the directory with the latest time stamp and in this case you can use the output of "ls -t" and snip off the first line - it will contain the youngest directory entry:

Code:
#!/bin/ksh

# uncomment the following line to watch the script run:
# set -xv

typeset fFileName=""
typeset fDirectoryBase="/path/to/your"
typeset fDirectory=""

# determine the latest directory in "$fDirectoryBase", then put the found name behind
# of what in "$fDirectoryBase" is, plus a separating "/" :

fDirectory="${fDirectoryBase}/$(ls -t ${fDirectoryBase} | head -1)"

ls ${fDirectory} | while read fFileName ; do
     # /path/to/yourcommand "${fFileName}" will cycle through the files
     print - "--------------------[ ${fFileName} ]--------------------------"
     grep "^ORA" ${fFileName}
done

If the directories you look at have to be sorted by their names and their time stamps do not correspond your task will be a bit more difficult. Write again in this case and i will show you a solution for this.

I hope this helps.

bakunin
# 3  
Old 10-04-2010
Hi,

thanks Bakunin.

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.
# 4  
Old 10-05-2010
Quote:
Originally Posted by sudhir_83k
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
You could use a more elaborate filename mask to search for:

Code:
ls -t CSB.[A-Z][a-z][a-z][0-9][0-9]_[0-9][0-9]:[0-9][0-9]

Search for "file globs" or "file globbing" to find out how this expression works.

I hope this helps.

bakunin
# 5  
Old 10-11-2010
Help me in shell scripting

Hi,

Code:
 
#catr.ksh
#!/bin/ksh

v=""
a=ls -ltrd CSB.*|tail -1
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

when i execute the above code it says.

Code:
./ora_error.sh[5]: -ltrd:  not found
./ora_error.sh[6]: a:  not found
rm: oraerr_output.txt: No such file or directory
grep: can't open THerrFile_2.err
grep: can't open THerrFile_3.err
grep: can't open THerrFile_4.err
grep: can't open THerrFile_5.err

please advice.

Last edited by Scott; 10-12-2010 at 09:23 AM.. Reason: More code tags
# 6  
Old 10-11-2010
One of the reason for the error: command substitution miss..try..

Code:
a=$(ls -ltrd CSB.*|tail -1)
cd $a

# 7  
Old 10-11-2010
Quote:
Originally Posted by sudhir_83k
Hi,

Code:
 
#catr.ksh
#!/bin/ksh

v=""
a=ls -ltrd CSB.*|tail -1
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

when i execute the above code it says.

./ora_error.sh[5]: -ltrd: not found
./ora_error.sh[6]: a: not found
rm: oraerr_output.txt: No such file or directory
grep: can't open THerrFile_2.err
grep: can't open THerrFile_3.err
grep: can't open THerrFile_4.err
grep: can't open THerrFile_5.err

please advice.
The line a=ls -ltrd CSB.*|tail -1 should be

a=`ls -ltrd CSB.*|tail -1`

Then

cd $a
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

2. Shell Programming and Scripting

Code to pick all files from a directory and send it to a command

(4 Replies)
Discussion started by: rossi
4 Replies

3. UNIX for Dummies Questions & Answers

To find the latest modified file in a directory

I am trying to fetch the latest modified file from a directory using the command find . -type f -exec ls -lt \{\} \+ | head | awk '{print $9}' After the O/P, I get the below mentioned error and the command doesnt terminate at all. find: ls terminated by signal 13 find: ls terminated by... (2 Replies)
Discussion started by: Sree10
2 Replies

4. Shell Programming and Scripting

to pick the latest file modified in a directory

I wan to pick the latest modified file name and redirect it to a file .. ls -tr | tail -1 >file but this is printing file ins side the filename , can anyone help me out (5 Replies)
Discussion started by: vishwakar
5 Replies

5. UNIX for Dummies Questions & Answers

How to pick only the latest files based on the timestamp?

I have a few log files which get generated on a daily basis..So, I need to pick only the ones which get generated for that particular day. -rw-r--r-- 1 staff 510732676 Apr 7 22:01 test.log040711 -rwxrwxrwx 1 staff 2147482545 Apr 7 21:30 test.log.2 -rwxrwxrwx 1 staff 2147482581 Apr 7 19:26... (43 Replies)
Discussion started by: win4luv
43 Replies

6. Shell Programming and Scripting

find the latest files in multiple directory

I want to get the latest files from multiple directories, d1, d2,d3 and d4 under the parent dierectoy d. can anyone help out with this? thx (3 Replies)
Discussion started by: shyork2001
3 Replies

7. UNIX for Dummies Questions & Answers

pick the very latest directory

Hi, I have some list of directories in the form datemonthyear e.g. 02082009, 03082009 and 04082009 etc. I need to pick the latest directory from the current working directory. Outcome: 05082009 This is the output am expecting. Thanks (6 Replies)
Discussion started by: venkatesht
6 Replies

8. Shell Programming and Scripting

how can i pick the latest log file as per below

in the below .. i want to pick the latest logfile which is having JPS.PR inside.. that means i want particularly "spgport040408041223.log:@@@@@@@@ 04:13:09 Adding: JPS.PR." which is latest among these.. is it possible to compare the current time with logfile time ? reptm@xblr0758rop>... (4 Replies)
Discussion started by: mail2sant
4 Replies

9. Shell Programming and Scripting

Pick the latest set of files

I have task in which I need to pickup a set of files from a directory depending on the following criteria: Every month 6 files are expected to arrive at /test. The files come with date timestamp and the latest file set for the month needs to be used Suppose this is the set of files that present... (5 Replies)
Discussion started by: w020637
5 Replies

10. Shell Programming and Scripting

script to find latest executable in particular directory and start that particular ex

i have a directory in which there are executable files and these files are added at runtime. now i need a shell script which will be called at a certain interval. this shell script should find the latest executable file in that directory and start that executable. care should be taken that once the... (6 Replies)
Discussion started by: kvineeth
6 Replies
Login or Register to Ask a Question