LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep greater than time period?? (https://www.linuxquestions.org/questions/programming-9/grep-greater-than-time-period-896764/)

nomiezvr4 08-11-2011 09:39 AM

grep greater than time period??
 
Hey guys, I'm fairly new at unix shell scripting and I have a quick question.
Quick overview I devolped a script where I generate a file ..and I want to grep any time greater than 30 minutes.
For example i run this which generates a file

./ggsci << endit >$AUDIT_DIR/check_output.out


The file contains:

EMMP 00:00:00 00:00:06
PMMP 00:00:00 00:01:09
RMDRA 00:00:00 00:00:00
RMDRB 00:00:00 00:01:06

I want to grep for any time greater than 30minutes..
How can I do that?

nomiezvr4 08-11-2011 09:51 AM

to give a better idea

Program Status Grp lag TimeChkpt

MANAGER

RUNNG
EXTRACT RUN EMMP 00:00:00 00:00:06
EXTRACT RUN PMMP 00:00:00 00:00:06
REPLICAT RUN RMDRA 00:00:00 00:00:00
REPLICAT RUN RMDRB 00:00:00 00:00:06


One column is lag, the other is time check point. If there is a lag and how big of a lag and check point time also...sometimes their is an error and the lag or check point is greater than 30minutes ..

Hope that helps

My thought process is that for a word its something like
grep -w 'Agent is Running and Ready' $AUDIT_DIR/check_2.out

so for time I want it to grep a time greater than 30mintes...

grail 08-11-2011 09:51 AM

Please explain further, you mean the difference between column 2 and 3 is greater than 30 or the difference between values in a column with relation to the following or preceding line?

nomiezvr4 08-11-2011 10:02 AM

what these numbers do is let us know if there is any sort of lag and how much lag. If there is a lag the numbers in column two will increase, if the system has not check pointed for a time frame the numbers in column 3 will increase.

usually the numbers will stay below 5 minutes and if the system is running real slow they might get to 20 minutes.

What i do is runa command to generates the below and puts it into a file:
I run
./ggsci << endit >$AUDIT_DIR/check_oracle_output_$ORACLE_SID.out

the file check_oracle_output_$ORACLE_SID.out contains info below


Program Status Grp lag TimeChkpt

MANAGER

EXTRACT RUN EMMP 00:00:00 00:00:06
EXTRACT RUN PMMP 00:00:00 00:00:06
REPLICAT RUN RMDRA 00:00:00 00:00:00
REPLICAT RUN RMDRB 00:00:00 00:00:06


What I wanted to do was grep the file for anytime greater than 00:30:00 and then i was going to set up an if clause and send myself an e-mail..
hope that helps

ta0kira 08-11-2011 10:21 AM

Instead of grep you might do this:
Code:

target='! ! ! ! 00:00:30'
{ echo; echo "$target"; cat 'the-file.txt'; } | sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d'

Basically $target is the max elapse with "!" as a place-holder for the first 4 fields. sort as called above will sort by the 5th field, then sed will delete all lines that come at or before $target. Incidentally, the output will be sorted by the 5th field. The extra echo is so sed works correctly if there isn't anything before $target.
Kevin Barry

nomiezvr4 08-11-2011 10:28 AM

Barry thanks for the info. Now this is to check for anything above 30 correct, not just 30 itself?

also for the way it would be is...
your code (target='! ! ! ! 00:00:30'{ echo; echo "$target"; cat 'the-file.txt'; } | sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d') then file name check_oracle_output_$ORACLE_SID.out


like
target='! ! ! ! 00:00:30'{ echo; echo "$target"; cat 'the-file.txt'; } | sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d' check_oracle_output_$ORACLE_SID.out



bare with me, im still a bit new

nomiezvr4 08-11-2011 10:30 AM

sorry feel like an idiot, just saw the-file.txt part

nomiezvr4 08-11-2011 10:33 AM

Is the code for anything above 30 or just at 30?

I have this
target='! ! ! ! 00:00:30'{ echo; echo "$target"; cat '$AUDIT_DIR/check_oracle_output_$ORACLE_SID.out';} | sort -t' ' -k5,5 -s | sed '
1,/! ! ! !/d'


and i am getting an error on: ./testgg.ksh[31]: syntax error at line 42 : `}' unexpected

ta0kira 08-11-2011 10:37 AM

Sorry, it needs to be two separate lines. Either that or insert ";" where the newline is. I've used "the-file.txt" in place of the input file and output is to standard output. This should give you everything 30s or greater. If you need the lines in their original order I can help with that, also.
Kevin Barry

grail 08-11-2011 10:40 AM

Wouldn't that be 30 seconds in the format it is in now? If the lost column is HH:MM:SS (is this correct) are we simply looking for any last column greater than 00:30:00?
If so, how about:
Code:

awk -F: '/RUN/ && ($(NF-2) > 0 || $(NF - 1) > 30)' check_oracle_output_$ORACLE_SID.out
Or if you really want to, skip the output file and just pipe your command into the front.

ta0kira 08-11-2011 10:46 AM

Quote:

Originally Posted by grail (Post 4439726)
Wouldn't that be 30 seconds in the format it is in now? If the lost column is HH:MM:SS (is this correct) are we simply looking for any last column greater than 00:30:00?

I was trying to provide a solution that allowed for arbitrary times, e.g. 01:23:45.
Kevin Barry

grail 08-11-2011 10:48 AM

Quote:

I was trying to provide a solution that allowed for arbitrary times, e.g. 01:23:45
As am I :)

nomiezvr4 08-11-2011 11:03 AM

I am looking for it minutes but i could tell that kevin had it in seconds
Now what im trying to do is basically see if the return status for the above is 0
if it is 0 then echo "time is greater than 30" > $AUDIT_DIR/file1.txt


so in other words could i do
target='! ! ! ! 00:30:00'
{ echo; echo "$target"; cat '$AUDIT_DIR/check_oracle_output_$ORACLE_SID.txt';}| sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d'

if [ $? -eq 0 ]
then
echo "time is greater than 30" > $AUDIT_DIR/file1.txt
fi

So if the target command does succeeds with something above 30 then the text time is greater than 30 would be inputted into the file1.txt...

Am I correct?

markush 08-11-2011 11:20 AM

Hi,

I've found this with grep only
Code:

grep -e "[3-9][[:digit:]]$\|[1-9]:[[:digit:]][[:digit:]]$"
for me it works, with this file
Code:

EMMP 00:00:00 00:00:06
PMMP 00:00:00 00:01:09
RMDRA 00:00:00 00:00:00
RMDRB 00:00:00 00:01:06
RMDRB 00:00:00 00:00:38

I got this output
Code:

PMMP 00:00:00 00:01:09
RMDRB 00:00:00 00:01:06
RMDRB 00:00:00 00:00:38

which as I understood is what you expected.

Markus

ta0kira 08-11-2011 11:28 AM

Quote:

Originally Posted by nomiezvr4 (Post 4439761)
I am looking for it minutes but i could tell that kevin had it in seconds
Now what im trying to do is basically see if the return status for the above is 0
if it is 0 then echo "time is greater than 30" > $AUDIT_DIR/file1.txt


so in other words could i do
target='! ! ! ! 00:30:00'
{ echo; echo "$target"; cat '$AUDIT_DIR/check_oracle_output_$ORACLE_SID.txt';}| sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d'

if [ $? -eq 0 ]
then
echo "time is greater than 30" > $AUDIT_DIR/file1.txt
fi

So if the target command does succeeds with something above 30 then the text time is greater than 30 would be inputted into the file1.txt...

Am I correct?

You would need to explicitly check for lines being output from sed. You also don't need the if statement.
Code:

target='! ! ! ! 00:30:00'
{ echo; echo "$target"; cat '$AUDIT_DIR/check_oracle_output_$ORACLE_SID.txt'; } | \
  sort -t' ' -k5,5 -s | sed '1,/! ! ! !/d' | grep -q . && \
  echo "time is greater than 30" > $AUDIT_DIR/file1.txt

Also, please use [CODE][/CODE] tags for your code or the # button.
Kevin Barry

PS Note that I've used "!" as a placeholder because it has the lowest ASCII of printing characters besides " ", which makes it come out on top of the other lines with a non-empty respective field (for future reference.)


All times are GMT -5. The time now is 05:43 PM.