regex - Why is Bash pattern match for ?(*[[:class:]])foobar slow? -
i have text file foobar.txt around 10kb, not long. yet following match search command takes 10 seconds on high-performance linux machine.
bash>shopt -s extglob bash>[[ `cat foobar.txt` == ?(*[[:print:]])foobar ]]   there no match: characters in foobar.txt printable there no string "foobar".
the search should try match 2 alternatives, each of them not match:
"foobar"   that's instantenous
*[[:print:]]foobar   - should go this:
should scan file character character in 1 pass, each time, check if next characters
[[:print:]]foobar   this should fast, no way should take millisecond per character.
in fact, if drop ?, is, do
bash>[[ `cat foobar.txt` == *[[:print:]]foobar ]]   this is instantaneous. second alternative above, without first.
so why long??
as others have noted, you're better off using grep.
that said, if wanted stick [[ conditional - combining @konsolebox , @rici's advice - you'd get:
[[ $(<foobar.txt)  =~ (^|[[:print:]])foobar$ ]]   edit: regex updated match op's requirements - thanks, @rici.
generally speaking, preferable use regular expressions string matching (via =~ operator, in case), rather [globbing] patterns (via == operator), primary purpose matching file- , folder names.
Comments
Post a Comment