Currently I am using the following command to search for 2 words in a file:
grep -r -e word1 -e word2
This loops recursively through all subdirectories, finds which files have word1, word2 (currently all files have both word1 and word2), and prints the lines that match. The problem is when it finds word1, it prints a newline with the filename that it found and the line. Then when it finds word2, it prints a newline with the filename that it found and the line. So the filename is printed twice.
So the output would be something like this:
file1: word1 ... rest of line
file1: word2 ... rest of line
But I want something like this:
file1: word1 ... rest of line word2 ... rest of line
Where the whitespace in between word1 and word2 could be a tab or some other delimeter.
I don't want to parse my original output and have to merge the lines together manually. I am looking for a command to do this in 1 go using bash.
If it is guaranteed that each file will have one and only one match for each word, you can do this: