[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1537
  • Last Modified:

grep log files in a date range for a string

I want to create a shell script that will have 4 variables passed in   one is a dir and the other is a time less 1 , 2 hours a string to search for and either print to the screen or write to a file.

For example

Script name  :check_log_files.ksh    
Var1 directory:  suppliers  
Var2 time - 1: -1    -2 -10    etc
Var3 grep: ORA-
Var4 write or print:    print

./check_log_files.ksh   suppliers -2   ORA- print

This would check the log files in /code/conversion/$1/log    for the word ORA- that were generated in the last two hours and print the file name and line with ORA- in it.    

./check_log_files.ksh   suppliers -2   ORA- write
This would do the same as above except write the output to a file. The file name would be
$1_$timestamp.txt

Only file of type .log and .lst are checked.

Thanks in advance.
0
dplinnane
Asked:
dplinnane
  • 5
  • 4
1 Solution
 
woolmilkporcCommented:
Pleas post examples of the .log and .lst entries. Obviously we need to know the format and location of the timestamp.

wmp
0
 
dplinnaneAuthor Commented:
-rw-r--r-- 1 jsmith consult   2305 2011-05-03 15:12 STO_no_chunk.skuo.20110503150236.log
-rw-r--r-- 1 jsmith consult   1354 2011-05-03 15:13 WH.skuy.20110503150001.log
-rw-r--r-- 1 jsmith consult   1356 2011-05-03 15:25 WH.skum.20110503150000.log
-rw-r--r-- 1 jsmith consult   2309 2011-05-03 16:37 STO_no_chunk.skuc.20110503150236.log
-rw-r--r-- 1 jsmith consult   1355 2011-05-03 18:00 WH.skus.20110503150000.log
-rw-r--r-- 1 jsmith consult   1354 2011-05-03 18:06 WH.skua.20110503150000.log
-rw-r--r-- 1 jsmith consult   2314 2011-05-03 19:19 STO_no_chunk.skul.20110503150236.log
-rw-r--r-- 1 jsmith consult   2318 2011-05-04 01:35 STO_no_chunk.skuy.20110503150236.log
-rw-r--r-- 1 jsmith consult   2317 2011-05-04 03:20 STO_no_chunk.skuk.20110503150236.log

I am just looking for ORA- errors mainly
here is an example
create.label_seq.sql.20110426184317.log:ORA-00955: name is already used by an existing object
0
 
woolmilkporcCommented:
Where is the timestamp? One line above? Please post more lines!

You might understand that we cannot know how old an entry is if there is not a timestamp somewhere.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
dplinnaneAuthor Commented:
5th column is date  2011-05-03
6th column is time   15:12  
0
 
woolmilkporcCommented:
So you don't want to see the ORA- messages created in the last n hours, but all ORA- messages in files having been updated n or less hours ago?

#!/bin/ksh
DIR="/code/conversion/$1/log"
AGE=$((${2#-}*60))
OP=$4
[[ $OP = "print" ]] && OP=""
[[ $OP = "write" ]] && OP=">${1}_$(date +%Y%m%d%H%M%S).txt"

find $DIR -type f -name "*.log" -o -name "*.lst" -mmin -$AGE | eval xargs grep $3 $OP

exit

There is no checking for correct input parameters.
Adding such checks could be a nice task for you to learn a bit of scripting!

wmp

0
 
dplinnaneAuthor Commented:
Almost perfect.
It seems that the -mmin is not working properly. It give me all the files in the directory regardless of time stamp.


When I try find *.log -type f -mmin -60
This works fine I get the expected results.
I modified GE=$((${2#-}*60))
to GE=$((${2#-}*1))
so that I am searching for files in minutes and not hours.

What does the -o mean in
og" -o -name "*.lst"

Thanks
0
 
woolmilkporcCommented:
"-o" means OR.

We search for files ending in .lst OR ending in .log, as you requested:
"Only file of type .log and .lst are checked."

wmp

0
 
dplinnaneAuthor Commented:
Thanks I though thats what it was but it seems to return all files in the directory like the -mmin -1 or -300 has no effect.
It will work for the way it is it just means a few hundred rows returned instead of 10 or 20
0
 
woolmilkporcCommented:
What is your OS?

"-o" and "-mmin" are pretty standard in almost any find implementations I'm aware of.

Maybe your find requires grouping (although that's implied with "-o)?

find $DIR  \( -name "*.log" -o -name "*.lst" \) -type f -mmin -$AGE | eval xargs grep $3 $OP

Please note that I put "-type f" behind the -name restrictions, to make it really textbook.



 
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now