perl - bash wildcard expansion with a set construct -


bash wildcard expansion.

i have direcotry 3 files in it. want use wilcar expansion match program.c , program.o

casper@casper-pc ~ $ ls -ltr total 4 drwxr-xr-x+ 1 casper none 0 apr 12 18:31 perl_lwp -rw-r--r--  1 casper none 0 apr 25 00:14 program.o -rw-r--r--  1 casper none 0 apr 25 00:14 program.c -rw-r--r--  1 casper none 0 apr 25 00:14 program.log 

this not work.

casper@casper-pc ~  $ ls -ltr | egrep program.[co] 

nor work.

 casper@casper-pc ~  $ ls -ltr | egrep program.? 

this not work

casper@casper-pc ~ $ grep -r program.? 

this work - wanted use bash brace expansion, not perl.

casper@casper-pc ~ $ ls -ltr | perl -nle 'print /(program.[co])/' program.o program.c 

however thought strange matches both of them because thought set construct match o or c , stop once matched

use set -x see happening. since using wildcards (glob constructs) outside quotes, the shell expands filenames before egrep gets run.

if want pass patterns program, enclose them inside 'single quotes'.

don't confuse globbing regular expressions - different pattern matching regimes. example:, in regular expressions ? means "zero or more of preceding pattern", whereas in globbing means exactly 1 character.

program.? in regular expression means "program" followed optional single character (. means 1 of character except newline) somewhere in string.

program.? in globbing means "program." followed 1 single character.

egrep takes regular expressions, not glob constructs.

this possibly unnecessary use of both ls , egrep, use echo:

echo program.[co]    program.c program.o echo program.?    program.c program.o 

this works because echo shell built-in , shell globbing (and more efficient calling external program ls).

now perl snippet worked 2 reasons. one: enclosed pattern inside single quotes, didn't egrep.

two: happened pick only pattern same in regular expressions , globbing - [ ] character-class notation (although there differences here). /.../ notation in perl invokes regular expression match (m) operator default.

you expected perl match stop on first match, -nl options means performs print statement in loop each line in standard-input, comes pipe.

btw, perl has built-in function glob().

echo *|perl -nle 'print glob("program.[co]")' 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -