[svlug] extended grep reg exp

William R Ward bill at wards.net
Tue Aug 12 13:37:12 PDT 2003


William R Ward writes:
>Robert Khachikyan writes:
>>I've read the doc for grep extensively and google searched
>>it and still couldn't find what i was looking for...on top
>>of that, i left my reg exp book @ home....so here it is.
>>
>>I have a big file that has
>>
>>3918400 bla bla bla
>>3918401 bla bla bla
>>3918402 bla bla bla
>>3918403 bla bla bla
>>3918404 bla bla bla
>>...
>>3945785 bla bla bla
>>3945786 bla bla bla
>>3945787 bla bla bla
>>...
>>
>>you get the idea. I want to grep a portion of it out.
>>Let's say from 3918403 -> 3928404 (10001 lines).
>
>That's not an easy problem to solve with regexp.  If it was 3910000 to
>3919999 however, it would be easy: 391[0-9]{4} (egrep syntax) or
>391[0-9][0-9][0-9][0-9] (grep syntax).

I forgot to add ... this would be easy in Perl.  This should do it:

perl -ne 'my($n)=split; print if $n >= 3918403 && $n <= 3928404'

Similar awk scripts would be similarly easy, I suspect.

Using regular expressions for this task is a bit like driving nails
with a screwdriver.  It can be done, but it isn't the best use of the
tool...

--Bill.

-- 
William R Ward            bill at wards.net          http://www.wards.net/~bill/
-----------------------------------------------------------------------------
           PROFESSIONAL PROGRAMMER, CLOSED COURSE.  DO NOT ATTEMPT.




More information about the svlug mailing list