LinuxQuestions.org
Review your favorite Linux distribution.
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 10-15-2010, 02:46 PM   #1
JamesEarlOwens
LQ Newbie
 
Registered: Oct 2010
Location: New York
Posts: 5

Rep: Reputation: 0
Can grep exclude symlinks ??


Hi

I tried to run one grep that excludes symlinks and pipe it into a second that searches for my pattern but I continue to get the recursive directory loop error. As a result (I think) my grep is incomplete as the search just loops between directories symlinked together.

grep -R -H -r "" * | grep -r class\=["'][^"']+["'] * > /log/cssSurvey/allClassTags.txt;

I've looked through this forum and the man pages but so far no luck! Thanks very much for your thoughts.

James

Last edited by JamesEarlOwens; 10-15-2010 at 02:57 PM. Reason: New info
 
Old 10-15-2010, 02:59 PM   #2
JamesEarlOwens
LQ Newbie
 
Registered: Oct 2010
Location: New York
Posts: 5

Original Poster
Rep: Reputation: 0
I also tried to find non link files and pipe them to the grep:
find . ! -type l | grep -r class\=["'][^"']+["'] * > /log/cssSurvey/allClassTags.txt;
But this stills results in the evil warning:
grep: warning: cssSym/cssSym: recursive directory loop

What works?
 
Old 10-15-2010, 03:04 PM   #3
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
both `find` and `grep -r` are recursive, so even if they plan to filter symlinks out of their potential results, they are still traversing through symlinked directories searching, hence the loop problem. (This is my take on it anyhow).

Therefore, maybe use `find` to do the recursing, but remove the -r from `grep` and see what happens -- if you're recursing using `find`, there's no need for `grep` to also recurse..

If find (continues to or does) have problem of recursive loop, you'll need to give it finer-grained instructions to eliminate the problem. I just tested, and while `grep` issues the warning about the recursive loop, the `find` command is immune. So, since you're grepping through files anyhow, you could use `find` to find all aptly-named files, and either use find's -exec option to run the grepping, or pipe the find output into `xargs` and do the grep from there.

Last edited by GrapefruiTgirl; 10-15-2010 at 03:18 PM.
 
Old 10-15-2010, 03:38 PM   #4
JamesEarlOwens
LQ Newbie
 
Registered: Oct 2010
Location: New York
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for your super fast reply! And thanks for your idea. However, when I remove the -r flag from the grep the search gets limited to the top level directory.
find . ! -type l | grep class\=["'][^"']+["'] * > /log/cssSurvey/allClassTags.txt;

When I run the find alone the output shows recursive searching but when I add the grep the search does not include sub-directories.

Thanks again
 
Old 10-15-2010, 03:41 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Code:
find . -type f -exec grep class\=["'][^"']+["'] {} \; > /log/cssSurvey/allClassTags.txt
Does this work? (Assuming your regex is good and you don't need any switches for grep to use the regex - I haven't considered that, as the problem appears to be a recursion issue)
 
Old 10-15-2010, 04:25 PM   #6
JamesEarlOwens
LQ Newbie
 
Registered: Oct 2010
Location: New York
Posts: 5

Original Poster
Rep: Reputation: 0
Holy cats, it worked! You nailed it Celine! Thanks so much! I really appreciate your help.

James
 
Old 10-15-2010, 04:30 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Hey that's great, I'm glad it works for you.

You can mark the thread "solved" using the "Thread Tools" menu just above the first post; that helps folks who are searching specifically for 'solved' threads.

Have a good day!
 
Old 10-15-2010, 04:35 PM   #8
JamesEarlOwens
LQ Newbie
 
Registered: Oct 2010
Location: New York
Posts: 5

Original Poster
Rep: Reputation: 0
Sure thing. I think I have some work left on the regex but that I can handle. Thanks again!
 
Old 10-20-2011, 10:59 AM   #9
dourk
LQ Newbie
 
Registered: Jul 2006
Posts: 21

Rep: Reputation: 0
same problem here

I want to try this trick but don't understand how. Which is the search string in this line?

Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
find . -type f -exec grep class\=["'][^"']+["'] {} \; > /log/cssSurvey/allClassTags.txt
 
Old 10-20-2011, 03:16 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
find . -type f -exec <command> {} \;
Locate all regular files in the current directory, and execute the given command on each one.

Code:
grep class\=["'][^"']+["']
The given command is grep, which searches for patterns inside files. The search pattern is the regular expression following the command name. In this case it finds lines that contain class="quoted string".

Code:
> /log/cssSurvey/allClassTags.txt
Redirect the output into the given file.

Does that help you?

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

A good regular expressions tutorial:
http://www.grymoire.com/Unix/Regular.html
 
Old 10-21-2011, 05:05 AM   #11
dourk
LQ Newbie
 
Registered: Jul 2006
Posts: 21

Rep: Reputation: 0
Thank you David

I think it will work now. I'll try it when I get home. I would never understand this from the grep man page
 
  


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
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 11:02 PM
[SOLVED] grep exclude Manuel-H Linux - General 4 10-12-2010 09:50 PM
CVS Exclude : Exclude sub directories from check out On Linux from command line shajay12 Linux - Newbie 1 08-03-2009 01:36 AM
Exclude certain patterns from grep? kinetik Linux - General 4 04-24-2006 06:37 AM
How can I filter the output of grep to exclude certain cases? QtCoder Linux - General 1 03-28-2004 01:05 AM

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

All times are GMT -5. The time now is 11:26 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