LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-13-2012, 07:21 AM   #1
krojther
LQ Newbie
 
Registered: Oct 2009
Posts: 20

Rep: Reputation: 0
How to grep multiple occurence of a substring


Dear All,
I have to find all subdirectory which name starts with substring log
I tried

Code:
find <root> -type d | grep -E "'log'{2}"
but it returns only directories with a name starting with "logg"
I need to find directories like these:

/dir1/dir2/gh/jh/log12/hgjds/hjsa/loger/...
or
/dir1/dir2/nmdsf/hjadfs/logger/hjdahjsh/logkj/...
and so on
Thanks
 
Old 09-13-2012, 07:42 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
Forget this post, wrong answer.

Last edited by Didier Spaier; 09-13-2012 at 07:43 AM.
 
Old 09-13-2012, 07:23 PM   #3
darqtanian
LQ Newbie
 
Registered: Aug 2011
Location: Philippines
Distribution: CentOS
Posts: 6

Rep: Reputation: Disabled
Try this

find <root> -name "*.logg*"
 
Old 09-13-2012, 07:30 PM   #4
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Does this find/grep work okay for you? Or if not, then perhaps you can provide some more examples of the lines that you want grep to match/reject.
Code:
find . -type d | grep '/log'
But looking at your grep above, I'm not sure I understand your purpose yet. The single quotes around 'log' are redundant, they do nothing much. The "{2}" applies only to the "g" character that precedes it. Whereas with a regexp like
Code:
grep -E '(log){2}'
then it will match any line with "loglog" in it instead.
 
Old 09-13-2012, 08:12 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
find . - type d -name 'log*'
 
Old 09-16-2012, 05:34 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The whole concept behind find is that it provides advanced options for locating files. There should rarely be any need to parse the output with grep or another tool.

Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

As mentioned before, it would help if you detailed your requirements more thoroughly, but I imagine something like this would work:

Code:
find . -type d -path '*/log*' -print
The -path option lets you match the entire path of the file, not just the name. The slash in the globbing pattern ensures that only directory names that start with "log" are printed. Finally, it's usually advisable to get in the habit of explicitly adding the -print action, even though it is the default action if nothing is given. There are some advanced search cases where it's needed. See the last section of the first link.

(
Edit: After reading more closely, it looks like you want to match paths that have two instances of "log" in them? In that case, just double the globbing pattern to:
Code:
find . -type d -path '*/log*/log*' -print
)

Finally, if you are going to use this list in a loop or similar command, be sure to use the -print0 option instead.

http://mywiki.wooledge.org/BashFAQ/020

Last edited by David the H.; 09-16-2012 at 05:42 AM. Reason: as posted
 
  


Reply



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
output the occurence number in sed or grep results in every regex match mbaste2 Linux - General 3 04-06-2011 01:58 AM
Using Grep/Awk/Sed to get a substring from a command johnjust Programming 5 01-12-2010 08:02 PM
grep for multiple targets mjtruco Linux - Newbie 14 05-24-2009 02:01 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:18 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
Open Source Consulting | Domain Registration