Go Back   The macosxhints Forums > OS X Help Requests > UNIX - Newcomers



Reply
 
Thread Tools Rate Thread Display Modes
Old 06-01-2012, 08:26 AM   #1
Ty
Prospect
 
Join Date: Jun 2012
Posts: 2
File searching with grep

I have a complex directory structure full of data-containing folders, and in each folder there is a single acqu.par file that gives parameters relevant to the data in its folder. I want to make a list of all the data directories that match all of 5 different parameters. In other words, I want a list of all the data folders that I have the have 5 specific conditions, all listed in the acqu,par file.

I've tried to work with grep to do this, including recursive searching and searching only *.par files (otherwise it takes forever since the directories are large). I've gotten the following to work:

grep -HRn --include=*.par "condition1" .
(I want the filelocations and line numbers, hence the -H and -n)

As far as I can tell, the way to get grep to implement an "AND" operator (list only files that match 1 AND 2 AND 3 AND...) is to pipe it along:

grep -HRn --include=*.par "condition1" . | grep -HRn --include=*.par "condition2" .

However, this seems so give an OR, rather than AND, interpretation, listing all files that match either cond1 or cond2. I've tried removing the options and location parameters as well, giving something like:

grep -HRn --include=*.par "condition1" . | grep "condition2" | ...

But this doesn't list any files while I am certain that files exist that match all the conditions. (I've checked and checked.)

Any suggestions on how to do a recursive search with grep using an effective AND operator and limiting the search by file type?

Cheers.
Ty is offline   Reply With Quote
Old 06-02-2012, 08:11 AM   #2
fracai
MVP
 
Join Date: May 2004
Posts: 2,012
Pipes don't work that way. When you pipe something, you're taking the output of the first command and piping it through the second. If all your conditions are on the same line you'd be fine. If they're on separate lines, you'll need to either have grep print additional context (see -A, -B, or -C) or inspect each file and store whether it satisfied each individual condition. Then AND those yourself.
fracai is offline   Reply With Quote
Old 06-02-2012, 03:01 PM   #3
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,941
You'll want to write a shell script (instead of trying to do it all on one command line).
In your shell script, you can run a grep command and record the result of that command in a variable. And then test (and combine) your variables later in the script.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 06-02-2012, 04:37 PM   #4
dmacks
All Star
 
Join Date: Dec 2004
Posts: 677
Something like this might work:

find . -name acqu.par | xargs grep -l "thing to match" | xargs grep -l "thing to match" | ... | xargs basename
dmacks is offline   Reply With Quote
Old 06-05-2012, 03:52 AM   #5
Ty
Prospect
 
Join Date: Jun 2012
Posts: 2
After taking the advice to put this into a shell script, I've come up with something that works for me. Admittedly, it's not elegant, but it works. Thanks for the help, all.


#############################
#!/bin/bash

dataFolder='/topLevelDataFolder/'
fileDir='/folderForOutputList/'
fileName1='list1.out'
fileName2='list2.out'
...

cond1='aaa = 100'
cond2='bbb = 2--'
...

grep -HRn --include=*.par "$cond1" "$dataFolder" | cut -f 1 -d : > "$fileDir$fileName1"
#the cut command is to remove the part of the grep output that contains the matched text.

cat "$fileDir$fileName1" | while read LINE
do
grep -Hn "$cond2" "$LINE" | cut -f 1 -d : >> "$fileDir$fileName2"
done
#############################

(The cat/line reading can be done as many times as necessary to catch all the strings for matching.)
Ty is offline   Reply With Quote
Reply

Tags
grep, recursive, search

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:39 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.