Search text in list of files

4

I'm trying to execute double search within files and return file names.

I'm using

find ./ -iname '*txt' | xargs grep "searchtext" -sl

to find file names with 'searchtext' in them.

Command is returning a list of files.

How can I find "othersearchtext" in those already found files and show them in the same fashion?

wormhit

Posted 2011-11-23T09:15:18.660

Reputation: 163

It's fine if the answer is below. Please don't add an answer to your question. Thanks! – slhck – 2011-11-23T09:59:16.973

Answers

5

Feed the result to another grep by using backticks or equivalent $().

If you want to keep the intermediate list, use tee

grep -l oranges $(find . -name "*txt" | xargs grep -l apples | tee apples.txt)

RedGrittyBrick

Posted 2011-11-23T09:15:18.660

Reputation: 70 632

Thanks! grep -l "othersearchtext" $(find . -iname '*txt' | xargs grep "searchtext" -sl) – wormhit – 2011-11-23T10:04:48.137

1

You can chain xargs grep …:

find . -iname '*txt' | \
    xargs grep -l "searchtext" | \
    xargs grep -l "othersearchtext" | \
    xargs grep -l "yetanothersearchtext"

Peter John Acklam

Posted 2011-11-23T09:15:18.660

Reputation: 486

0

This bash will search multiple strings in given folder recursively and return filenames

Usage:

findfiles /var/www/ php searchText1 searchText2 searchText3 searchText4

bash: findfiles

#!/bin/sh

DIR=$1
EXT=$2

CMD="find $DIR -iname '*.$EXT' | xargs grep -l '$3' | xargs grep -l '$4' | xargs grep -l '$5' | xargs grep -l '$6' | xargs grep -l '$7' | xargs grep -l '$8' | xargs grep -l '$9'"

eval $CMD

wormhit

Posted 2011-11-23T09:15:18.660

Reputation: 163