LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-20-2014, 12:31 PM   #1
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
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)?
 
Old 02-20-2014, 12:38 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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'
 
Old 02-20-2014, 12:55 PM   #3
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian
Posts: 1,054

Rep: Reputation: 276Reputation: 276Reputation: 276
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.

Last edited by sgosnell; 02-20-2014 at 01:06 PM.
 
Old 02-20-2014, 01:01 PM   #4
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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?
 
Old 02-20-2014, 01:03 PM   #5
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sgosnell View Post
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".
 
Old 02-20-2014, 04:05 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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.
 
1 members found this post helpful.
Old 02-21-2014, 11:14 PM   #7
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Original Poster
Rep: Reputation: Disabled
Rknichols, thanks, it works this way. Are there any names for different syntaxes of regex that we have in bash vs in grep?
 
Old 02-22-2014, 12:00 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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.
 
1 members found this post helpful.
Old 02-22-2014, 08:20 AM   #9
yo8rxp
Member
 
Registered: Jul 2009
Location: Romania
Distribution: Ubuntu 10.04 Gnome 2
Posts: 102

Rep: Reputation: 29
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

Last edited by yo8rxp; 02-22-2014 at 08:22 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
CentOS-Asterisk- /var/spool/asterisk/monitor How to decipher who's call is who's? tananthulus Linux - Newbie 2 12-08-2009 05:50 AM
LXer: Installing The Asterisk PBX And The Asterisk Web-Based Provisioning GUI On Linu LXer Syndicated Linux News 0 09-25-2007 03:50 PM
asterisk configuration to make a call PC - PC through asterisk priya001 Linux - Server 0 11-08-2006 07:06 AM
LXer: VoIPowering Your Office with Asterisk: Shiny New Asterisk 1.4 LXer Syndicated Linux News 0 11-03-2006 07:54 AM


All times are GMT -5. The time now is 09:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration