General
cancel
Showing results for 
Search instead for 
Did you mean: 

grep dates on snmp collected data

Seun Ewulomi_1
Honored Contributor

grep dates on snmp collected data

Hi people,

I use egrep to grep on data of the format below

10/11/2003 00:00 299 0.43 0.45 140.54 68.34 72.2 32
11/11/2003 00:05 301 0.25 0.34 93.56 39.77 53.78 28
12/11/2003 00:10 300 0.34 0.37 113.91 54.08 59.83 32

1) To see all data for just a certain day I usually do
grep '^10/.*/2003'

2)to see all data for some days I usually do
egrep -e '11/.*/2003' -e '12/.*/2003' snmp.log

Now if I want to see the all data for example from 10/11/2003 - 15/11/2003 how would this be done with awk, egrep or sed.

My main aim is to see/filter all data in a snmp log file just for mondays-fridays and omitting all the weekend dates.

Any help or advice will be greatly appreciated.

regards,
seun
Jesus Christ is LORD
5 REPLIES
Martin P.J. Zinser
Honored Contributor

Re: grep dates on snmp collected data

Hello Seun,

egrep '^1[0-5]/.*/2003' snmp.log

will get the 10th to the 15th.
If you reallz want to know Mo to Fr I suggest you write a little Perl program to get the weekday of a given date and the take it from there.

Greetings, Martin
Stuart Browne
Honored Contributor

Re: grep dates on snmp collected data

I'd have to agree with Martin on this one (although his grep is good).

As all the entires should be in sequential date/time order, it should be easy to do a simple check against the first field and print it if it exceeds the given date pair provided.
One long-haired git at your service...
Mark Grant
Honored Contributor

Re: grep dates on snmp collected data

For a little help on the above mentioned script, it might be useful to know that if you take the year, month and day, in that order and find a mod 7, you get the day number (with Sunday being a 0)

for example, taking your first date of 10/11/2003 you reverse the date and take a mod 7

20031110%7 which gives you 1, so that date is a Monday.
Never preceed any demonstration with anything more predictive than "watch this"
Martin P.J. Zinser
Honored Contributor

Re: grep dates on snmp collected data

Hello,

as for a Perl solution, you could have a look at the Date::Manip module, which does provide the handy Date_IsWorkDay function for such purposes...

Greetings, Martin
Cristian Draghici
Frequent Advisor

Re: grep dates on snmp collected data

Here you go:
[diciu@bluefish diciu]$ cat tt
10/11/2003 00:00 299 0.43 0.45 140.54 68.34 72.2 32
11/11/2003 00:05 301 0.25 0.34 93.56 39.77 53.78 28
12/11/2003 00:10 300 0.34 0.37 113.91 54.08 59.83 32

[diciu@bluefish diciu]$ cat tt.sh
cat tt | while read sw
do
weekday=`echo $sw | cut -b0-10 | xargs date +%a -d `
ismatch=`echo $weekday | egrep -v "(^Sun$)|(^Sat$)"`

if [ "$ismatch" ]; then
echo $sw
fi
done

[diciu@bluefish diciu]$ sh tt.sh
11/11/2003 00:05 301 0.25 0.34 93.56 39.77 53.78 28
12/11/2003 00:10 300 0.34 0.37 113.91 54.08 59.83 32

tt is the data file.
tt.sh is the filter script (also attached in case the forum eats special characters).