LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using asterisk in grep (https://www.linuxquestions.org/questions/linux-newbie-8/using-asterisk-in-grep-4175495666/)

Mr. Alex 02-20-2014 11:31 AM

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"
I won't find anything. Along with "-i" option I tried to use "-G", "-P", "-E", "-e", "-w", "-x". What is the way to use asterisk in grep as in bash in general (with pipes)?

grail 02-20-2014 11:38 AM

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'

sgosnell 02-20-2014 11:55 AM

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.

Mr. Alex 02-20-2014 12:01 PM

Quote:

Originally Posted by grail (Post 5121742)
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'

What I'm trying to achieve is to use "grep" in "find" like this:

Code:

find / -iname "*vlc*tar*"
This finds cached vlc packages for pacman to install. I didn't understand your point in your reply. What would be a way to use grep with pipes in

Code:

find / -iname "*vlc*tar*"
? It just makes searches easier to type for me when I do a lot of them one following another.

Also, this is useful for general "grep" usage. For example,

Code:

cd /dev; ls -l sd*
shows everithing in /dev that starts with "sd". But

Code:

cd /dev; ls -l | grep "sd*"
shows whole another picture. Why?

Mr. Alex 02-20-2014 12:03 PM

Quote:

Originally Posted by sgosnell (Post 5121751)
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 ?.

In general bash usage I can type in "ford*cars" and it will mean "ford[anything or nothing here]cars".

rknichols 02-20-2014 03:05 PM

What you want is "any number of any characters". "Any character" is ".", so your expression should be
Code:

grep -i "ford.*cars"
You might want to include "edge of words" in the match so as not to get a hit on "I can't afford to buy that Johnny Carson memorabilia."
Code:

grep -i '\bford\b.*\bcars\b'
Note the use of single quotes so that the backslash characters are passed unmolested by the shell.

Mr. Alex 02-21-2014 10:14 PM

Rknichols, thanks, it works this way. Are there any names for different syntaxes of regex that we have in bash vs in grep?

rknichols 02-21-2014 11:00 PM

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.

yo8rxp 02-22-2014 07:20 AM

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.