hi anyone
how can use grep with wildcard. for example grep "sample?txt" filename doesn't show sample1txt or grep "sample*txt" filename doesn't show sample123.txt that there is in filename.
many thanks
samad
hi anyone
how can use grep with wildcard. for example grep "sample?txt" filename doesn't show sample1txt or grep "sample*txt" filename doesn't show sample123.txt that there is in filename.
many thanks
samad
grep uses regexes, not globs, with slightly different meanings. In glob, * means 'zero or more characters', in regex, it means 'zero or more of the previous character'.
So something* in regex terms would match something, somethingg, somethingggggggggggggggggggggg, somethin, but not somethina.
In regex, ? means "zero or one of the previous character" while . means "any character". You can combine the two as .? to mean "zero or one of any character" for example.
So try grep 'sample.txt'
to match sampleatxt, samplebtxt, samplectxt, etc.
Thanks
so, What is the way to find the following words in grep:
e.g: sample123.txt sampleahgdtxt
With regards to post #1, that would be:
sample.txt
and
sample.*txt
respectively
Hmm - it would match "somethina" unless terminated by e.g. line end $
or something else...
Yes it would match the somethin in somethina.
Often you can improve that with grep -w (word match), then somethina or somethinga or sample.txta or asample.txt would not match.
That will work only if your grep
is a non-POSIX grep
, because this is a (GNU, i believe) non-standard extension (see here).
bakunin
GNU, but also BSD grep...
... and SysV. And all derivates i.e. most if not all commercial Unix.
Many thanks
how can i find any help for regex e.g in "man command in terminal"?
test is:
somethin
somethina
somethinabc
something
somethinggg
grep "something*" test
somethin
somethina
somethinabc
something
somethinggg
somethina !?
somethinabc !?
but
grep "something?" test
no thing find
man regexp
gives you a comprehensive description of what is in the libc (that C-compiled programs use).
The trailing g*
means " g
zero or more times" and is useless unless there is a $
anchor (line end):
g*$
forces any number of g
until the line end.
The g?
in the meaning of " g
zero or one time" is only defined for ERE (extended regular expression), used with egrep or grep -E and awk and most modern languages.
"grep uses regexes, not globs" is this mean extended regular expression is used by grep?
and globs means basin regular expression?
many thanks
samad
--- Post updated at 05:52 AM ---
but something* match "somethinga" that a in somethinga isn't previous character!
--- Post updated at 06:10 AM ---
you say " but not somethina" this is incorrect because the something* match somethina.
[samad@localhost test]$ grep "something*" test
somethina
As I said already, grep something*
without an ending anchor is like grep somethin
because there may be zero g
s and any characters may follow.
The shell and find -name use glob expression.
grep uses basic regular expression.
egrep (or the equivalent grep -E) use extended regular expression.