LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 08: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, 08:42 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2beta2 on Lenovo Thinkpad W520
Posts: 6,777

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

Last edited by Didier Spaier; 09-13-2012 at 08:43 AM.
 
Old 09-13-2012, 08: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, 08: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, 09:12 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Code:
find . - type d -name 'log*'
 
Old 09-16-2012, 06:34 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 06:42 AM. Reason: as posted
 
  


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


All times are GMT -5. The time now is 06:29 AM.

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