[NTLUG:Discuss] interval regexp
Chris Cox
cjcox at acm.org
Tue Mar 19 21:46:22 CST 2002
Test Account 1 wrote:
>
> Can any regexp gurus tell me how to use
> the {min, max} syntax in an egrep search?
> e.g. if I want to get the first 4 matches
> of 'abc' in a file, what will the egrep
> (or any other flavor of grep) statement
> look like?
Use grep -E instead of egrep. Though there is
not supposed to be that much difference between
the two, I've found the current implementation
of egrep to be lacking. e.g.
grep -E '(hello ){2,4}'
Would match:
hello hello there
but not
hello hello
unless the second hello was followed by a space.
grep -E '^(hello ){2,4}there'
Would match:
hello hello hello hello there
but not
hello hello hello hello hello there
(note it is important that the pattern be anchored
somehow at the front... in this case, the anchor was
the beginning of the line '^' in order for the maximum
case to work properly.)
If you're looking for total matches across lines, you
probably should look at awk or sed... or
perl (or other higher level language).
...or pipe a grep to a head -4.
grep 'hello' | head -4
Returns the first 4 lines containing hello.
Hope that helps,
Chris
> Thanks,
> Sameer
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
More information about the Discuss
mailing list