Computing.Net > Forums > Unix > Help with grep

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with grep

Reply to Message Icon

Name: jb60606
Date: October 3, 2005 at 11:14:18 Pacific
OS: Suse
CPU/Ram: opteron/8GB
Comment:

First off, I am not very skilled in UNIX. I need to grep the contents of a sar file for entries between the hours of 8:30am and 10:30am.

a typical line in the file looks like this:

06:05:01 AM all 0.12 0.00 0.07 0.00 99.82


I've paged through some grep docs and the man pages on the net for a little advice, but I'm not havng much luck. Any ideas?

Thanks in advance



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 3, 2005 at 15:23:57 Pacific
Reply:

You are asking for a solution that is very tedious. Unix doesn't handle date arithmetic very well - especially when the time is given in AM/PM.

Here's where I would start:

1) create your sar file - something like:

sar -o /tmp/data.file

2) use sar's -h option to provide the time in seconds from the epoch:

sar -h -f /tmp/data.file

Now, you can more easily perform arthmetic on the date. You still have the problem in getting the number of epoch seconds for 0830 on any given date, but that's easier than messing with AM/PM times. Since you are using GNU tools, start with the unix date command %s formatting option.


0

Response Number 2
Name: anupam
Date: October 3, 2005 at 23:37:16 Pacific
Reply:

I hope that this script will do your job,if the script is in the exact format that you have given here.


terminal=`tty`
exec < prob1
while read line
do
line1=`echo $line|grep AM|cut -d" " -f1`
if [ ! -z $line1 ]
then
hr=`echo $line1|cut -d":" -f1`
min=`echo $line1|cut -d":" -f2`
if [ \( $hr -eq 08 -a $min -ge 30 \) -o \( $hr -eq 10 -a $min -le 30 \) -o \
( $hr -gt 8 -a $hr -lt 10 \) ]
then
echo $line
fi
fi
done
exec < $terminal

if you don't understand anything anywhere,just ask.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help with grep

need help with grep.... www.computing.net/answers/unix/need-help-with-grep/7366.html

Help with grep www.computing.net/answers/unix/help-with-grep/4821.html

problem with grep in a for loop www.computing.net/answers/unix/problem-with-grep-in-a-for-loop/3967.html