Using asterisk in grep
Hi guys. Can't find a way to use asterisk in grep as a character to mean "nothing or anything". Like if I create a dir "Ford Cars" somewhere in filesystem and try to find it like this
Code:
find / | grep -i "ford*cars" |
Well I see a few issues:
1. * in a regex means zero or more of the preceding character. So in your example this would be zero or more of the letter 'd' 2. find could have just easily been used to track down the item you were looking for, btw this would have then been the correct use of * as it would be globbing, ie 'd' followed by anything and then 'cars' |
The * should go at the end of the search expression, like ford*. If you add more characters after the *, you won't get the results you expect. Inside a string, you don't use an asterisk, you use ?. However, in grep, AFAIK, you would probably need to use ??, since there is a \ before the space, as in "ford\ cars", that gets inserted. You won't find "ford cars" in a directory listing, you will instead see "ford\ cars". The best I can suggest is to man grep.
|
Quote:
Code:
find / -iname "*vlc*tar*" Code:
find / -iname "*vlc*tar*" Also, this is useful for general "grep" usage. For example, Code:
cd /dev; ls -l sd* Code:
cd /dev; ls -l | grep "sd*" |
Quote:
|
What you want is "any number of any characters". "Any character" is ".", so your expression should be
Code:
grep -i "ford.*cars" Code:
grep -i '\bford\b.*\bcars\b' |
Rknichols, thanks, it works this way. Are there any names for different syntaxes of regex that we have in bash vs in grep?
|
Filename expansion, aka "globbing", in bash isn't regex at all. It has its own syntax, which is quite limited in comparison with regex. Regex itself does have several variants, the principal ones being "basic" and "extended", plus an "emacs" version, used by the emacs text editor, that isn't quite the same as the other two. None of those are like filename expansion.
|
if willing to grep * in expression , then put backslash in front of it like --> grep "some\*sommers"
sorry rknichols , i just seen your posting , you are right ! Please mark this as Solved |
All times are GMT -5. The time now is 11:20 PM. |