egrep help

Hi there,

Im having some issues using egrep, I have a text file containing server logs:

the user imputs 2 arguments, which are error checked and made into $searchMonth $searchYear respectivley.

I then do the grep command:

egrep /$searchMonth/ $file | egrep /$searchYear: | wc -l

to list how many occurances of the paticular searchmonth in the searchYear

However it seems to be matching anything even if its in the path name in the server log, and I only just need the 2 fields, therefore its not returning the true results.

Im not really familiar with egrep, so any advice would be appreciated.

That's what grep does; you need to construct a regular expression which indicates which part of the line to match if you don't want to match anywhere in the line. It can't magically guess which part of the line you want the match to be in.

If you don't want to spend too much time on learning this stuff, there's a lot of log grepping tools out there, but it's also easy to roll your own.

awk is probably easier to approach, but this is perfectly doable in egrep, too.

egrep -c "\\[[0-9]*/$searchMonth/$searchYear:" "$file"

This regular expression searches for literal opening square bracket (backslashed to make it literal, because opening square bracket otherwise has a special meaning in regular expressions; doubled the backslash, because the backslash has special meaning to the shell in a double-qutoed string -- sorry if I'm going too fast :slight_smile: followed by any number, any number of times, followed by slash, followed by the search month, followed by slash, followed by the search year, followed by a colon.

egrep -c counts the number of matches, so you don't need the pipe to wc -l

You were already pretty close; merely combining the month and the year expression would already drastically reduce the number of false matches. Actually that's probably quite sufficient.

egrep -c /$searchMonth/$searchYear: "$file"