[PLUG] simple grep/regex question

Robert Citek robert.citek at gmail.com
Mon Apr 14 19:14:02 UTC 2008


On Mon, Apr 14, 2008 at 1:54 PM, Jeme A Brelin <jeme at brelin.net> wrote:
>  On Mon, 14 Apr 2008, Galen Seitz wrote:
>  > I've tried these:
>  >  grep '[^;]*bit' foo.asm   # matches all bit
>
>  That's because the "bit" is preceeded by one or more non-; characters.
>
>  Make sure you match the start of the line in your regex and you're good:
>
>  grep "^[^;]*bit" foo.asm  # matches what you need!

A little trick I like to use to help me design REs is to add the
--color option to grep:

$ grep --color '[^;]*bit' foo.asm
$ grep --color '[^;]+bit' foo.asm
$ grep --color "^[^;]*bit" foo.asm

The --color option shows me the pattern that matched.  If I want
context, I'll use one of the -A, -B, or -C option, or I'll tack on an
extra expression -e ^.  For example:

$ grep -C 100 --color "^[^;]*bit" foo.asm
$ grep --color -e "^[^;]*bit"  -e ^ foo.asm

And if there's a lot of data, I'll adjust --color and pipe the output into less:

$ grep --color=always -e "^[^;]*bit"  -e ^ foo.asm | less -r

Good luck and let us know how things go.

Regards,
- Robert



More information about the PLUG mailing list