Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More Efficient Regexp?

Status
Not open for further replies.

jouell

MIS
Nov 19, 2002
304
US
Hi All,

Can anyone make this regexp more efficient?

I am trying to grep for 2008:09:45 through 2008:10:45 am in a file:

egrep -e "2008:09:4[5-9]" -e "2008:09:5[0-9]" -e "2008:10:[0-3][0-9]" -e "2008:10:4[0-5]" FILE

other than just:

egrep -e "2008:09:4[5-9]|2008:09:5[0-9]|2008:10:[0-3][0-9]|2008:10:4[0-5]" FILE

Thanks!
-jouell

 
Hi

While you are trying to process numeric values with text utility, I see no nicer way.

But if the values are in chronological order without unwanted lines between them, you can get them as an interval :
Code:
sed -n '/2008:09:45/,/2008:10:45/p' FILE

Feherke.
 
Unfortunately the sed solution stops at the first occurrence of 2008:10:45. I presume you would want it to continue printing if there are multiple occurrences.

I don't think you can make the egrep/b] expression much more efficient than that. Is it performance you're trying to improve, or code brevity/readability?

Annihilannic.
 
Hi All

Thanks for the feedback. Good Question Annihilannic. I was looking to improve the code brevity and readability.

(Looks like this may be the best we can do with this setup)


Thanks!
-jouell
 
Here are a couple of alternatives, but I don't think they're much clearer in the end:

Code:
egrep "2008:(09:(4[5-9]|5[0-9])|10:([0-3][0-9]|4[0-5]))" FILE
awk '{t=$1;gsub(":","",t)} t>=20080945 && t<=20081045' FILE
awk '$1>="2008:09:45" && $1<="2008:10:45"' FILE

Note that the awk solution is a lot less efficient than egrep, so if you're dealing with a significant amount of data, probably best avoided.

The second awk solution using string comparisons may be error-prone, you might want to test it more thoroughly than I have.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top