[ILUG] regex newbie question
Rory Browne
rbmlist at gmail.com
Fri Aug 8 16:41:46 IST 2008
2008/8/8 Hanafin, Jenny <jenny.hanafin at nuigalway.ie>
> Hi guys,
>
> I'm trying to come to grips with grep and regular expressions and while I'm
> making progress, I've come up with some things that puzzle me. Can someone
> explain to me why grep is returning the following, given the regex I'm
> using:
>
> [131] ls /data/gps/TEMP
> BELF160z.08o BELF.apr dwnld.lst hers1600.08d itrf05_eura.apr
> lfile.VLNT MACE160z.08n MACE160z.08o MACE208o.08n MACE208o.08o
> MACE213h.08n MACE213h.08o mace.lst
> VLNT160z.08n VLNT160z.08o VLNT213l.08n VLNT213l.08o VLNT.apr
> vlnt.lst
> [132] ls /data/gps/TEMP | grep
> '^[[:alpha:]]\{4\}[0-9]\{3\}[a-x]\.[0-9]\{2\}[o|n]$'
> [133] ls /data/gps/TEMP | grep
> '^[[:alpha:]]\{4\}[0-9]\{3\}[a-x]\.[0-9]\{2\}[o|n]'
> MACE213h.08n MACE213h.08o mace.lst VLNT160z.08n VLNT160z.08o
> VLNT213l.08n VLNT213l.08o VLNT.apr vlnt.lst
>
> What I want is an expression that only returns filenames with a specific
> format: 4 alphabetic characters, followed by 3 numeric, 1 alphabetic (a-x
> only), a period, 2 numeric characters and an "o" or an "n" at the end. This
> is what I think line [132] should return, and I don't understand why the
> first expression doesn't match any of the filenames. I also don't understand
> why filenames like 'vlnt.lst' are matched following the command in line
> [133].
>
> The problem seems to be with the [0-9] and I've tried using [[:digit:]] and
> [\d] instead but neither of those give me the answers I expect either!
>
> Any help appreciated, expecially at 4:30 on a Friday ;-)
>
> Thanks,
>
> Jenny
Grep isn't the best at regex on its own. Try grep -E, or ( if you're on a
reasonably modern machine ) grep -P.
Failing that ( and I know invoking perl might be overkill, but it provides
you with consistent regex syntax )
ls | perl -nle 'print if(/[a-z]{4}\d{3}[a-x]\.\d\d[on]$/)'
More information about the ILUG
mailing list