-1

I need to query java files which have both the strings 'rESTWSLib' and '500'. Both the strings could be anywhere in the document (i.e. line) and can be positioned anywhere on 0-infinite lines (position 0-n).

How to grep query for 'rESTWSLIB':

find -iname '*.java' -type f -exec grep -l "rESTWSLib" '{}' \; > ~/SMCrestwsList1.txt

output:

./some/file1.java
./some/file2.java

How to grep query for '500':

find -iname '*.java' -type f -exec grep -l "500" '{}' \; > ~/SMCrestwsList2.txt

output:

./some/file2.java
./some/file3.java

I need to combine both grep queries (or their results) so I am expecting this as result:

./some/file2.java

I have tried the solutions given in How to grep query all files for 2 strings

find -iname '*.java' -type f -exec grep -l "rESTWSLib" '{}' + | xargs grep -l "500" \; > ~/SMCrestwsList3.txt

But, since few files have spaces in their names, only partial output is given with the below error messages:

grep: ;: No such file or directory
grep: ./cdrm/bat/OpportunityManagement/Verify: No such file or directory

The filename is 'Verify something something'.

How can I tweak this to accept the spaces in the filename?

Community
  • 1
  • 1
AutoTester999
  • 528
  • 1
  • 6
  • 25

2 Answers2

1

Try using regex:

find -iname '*.java' -type f -exec grep -lzo "rESTWSLib.*500\|500.*rESTWSLib" '{}' \; > ~/SMCrestwsList1.txt
zwer
  • 24,943
  • 3
  • 48
  • 66
1

To allow filenames with spaces to be processes successfully, you should modify your pipeline by using grep -Z and xargs -0. These matching options change grep to print filenames separated by a \0 character and change xargs to expect null-separated input strings as well.

find . -exec grep -lZ "expr_one" {} \+ | xargs -0 grep -l "expr_two"
Grisha Levit
  • 8,194
  • 2
  • 38
  • 53