tags:

views:

34

answers:

2

I have a series of index files for some data files which basically take the format

index file : asdfg.log.1234.2345.index

data file : asdfg.log

The idea is to do a search of all the index files. If the value XXXX appears in an index file, go and grep its corresponding data file and print out the line in the data file where the value XXXX appears.

So far I can simply search the index files for the value XXXX e.g.

find . -name "*.index" | xargs grep "XXXX"     // Gives me a list of the index files with XXXX in them

How do I take the index file match and then grep its corresponding data file?

+2  A: 

Does this do the trick?

find . -name '*.index' |
xargs grep -l "XXXX" |
sed 's/\.log\.*/.log/' |
xargs grep "XXXX"

The find command is from your example. The first xargs grep lists just the (index) file names. The sed maps the file names to the data file names. The second xargs grep then scans the data files.

You might want to insert a sort -u step after the sed step.

Jonathan Leffler
thanks Jonathan - thats pretty much what i'm after the inital sed didn't fully so I replaced it with. Thanks tho sed 's/\.log.*/.log/'
wmitchell
This will fail if your files are called names with space, ' or ". To see why http://en.wikipedia.org/wiki/Xargs#The_separator_problemWith GNU Parallel http://www.gnu.org/software/parallel/ the separator problem is limited to filenames containing \n.find . -name '*.index' |parallel grep -l "XXXX" |sed 's/\.log\.*/.log/' |sort -u | parallel grep "XXXX"Watch the intro video to GNU Parallel at http://www.youtube.com/watch?v=OpaiGYxkSuQ
Ole Tange
@Ole: agreed that this runs fouls of spaces, newlines, and so on in file names - however, the question is posed in a context which presupposes the absence of such complications (it uses plain `find`, not `find ... -print0`, and it uses plain `xargs` not `xargs -0`), so the answer given works in the same environment.
Jonathan Leffler
A: 
grep -l "XXXX" *.index | while read -r FOUND
do
   if [ -f "${FOUND%.log*}log" ];then
      grep "XXXX" "$FOUND"
   fi
done 
ghostdog74