1
./folder1/file1:3.100 ... 
./folder1/file1: 3.99.5

./folder1/file2:... 
./folder1/file2: 3.100

./folder1/file3:3.99.5 
./folder1/file3: ...

./folder2/file1:3.99.5 
./folder2/file1: ...

./folder2/file2:3.100 ... 
./folder2/file2: 3.99.5

./folder2/file3:...
./folder2/file3: 3.100

Please explain, how to grep patterns "3.100" AND "3.99.5" in files in all subfolders, to get list of matched files:

folder1/file1
folder2/file2

Thanks in advance !:)

brunorey
  • 2,135
  • 1
  • 18
  • 26
nmr50
  • 51
  • 1
  • 8
  • 1
    Possible duplicate of [How to use grep to match multiple strings in the same line?](https://stackoverflow.com/questions/4487328/how-to-use-grep-to-match-multiple-strings-in-the-same-line) – Sundeep Jan 31 '19 at 11:09
  • the question is not clear whether the search terms are in single line or multiple lines, the duplicate has solutions for both cases – Sundeep Jan 31 '19 at 11:10
  • I meant to match files with BOTH patterns only... that is why I wrote folder1/file1 and folder2/file2 as example. – nmr50 Jan 31 '19 at 11:13

1 Answers1

2

Grep all files, print only names of matching files and rescan with xargs grep:

grep '3\.100' * -r -l  | xargs -d '\n' grep -l '3\.99\.5'
brunorey
  • 2,135
  • 1
  • 18
  • 26
  • 1
    Oh, I see you've edited your question to make the files mutli-line. My answer is no longer valid. – brunorey Jan 31 '19 at 12:44
  • Yeah, I did it to cut off "simple grep | grep" answer :) The main Q: is to grep those 3.100 and 3.99.5 args inside .mp3 files, that is why I modified my question:) – nmr50 Jan 31 '19 at 12:45
  • Looks like there is a (small?) problem with files those have space in their filenames... Could you, please, modify your method to operate files with spaces? – nmr50 Jan 31 '19 at 13:07
  • 1
    Sorry, you are right. I added the `-d '\n'` directive as described [here](https://stackoverflow.com/questions/16758525/make-xargs-handle-filenames-that-contain-spaces). Try it now. – brunorey Jan 31 '19 at 13:09
  • Sorry for delay - Yes, this updated algo is working as I wanted - THANK YOU very much !!:) And.. heck, I have 14 reputation points so I cannot vote for your great answer, sorry again! :/ – nmr50 Jan 31 '19 at 14:52
  • Ppl, how can I close this Q: as solved one? Thanks in advance !:) – nmr50 Jan 31 '19 at 19:55
  • 1
    You should [accept the answer](https://stackoverflow.com/help/someone-answers). Also see [this example](https://meta.stackexchange.com/a/5235) – brunorey Jan 31 '19 at 20:24
  • 1
    BTW, "*" should be replaced with "." - it gives normal mode to scan current folder with subfolders:) – nmr50 Feb 01 '19 at 11:41