Search code examples
shellgrepcut

Run second grep on each string returned by first grep


I have two files file1 and file2 and a pattern pattern.

Using grep on pattern in file1 and cut, I generate some strings like following:

String1
String2
String3
String4
String5

For each of these strings, I have to grep file2. How can I do that?

For now, I am doing this manually, but a better method must exist. I can probably do this using a for loop, but I wonder if just piping the results of first grep and cut to second grep can work anyhow.

Note: Please let me know if any other information is required.


Solution

  • You can use process substitution with -f option:

    grep -Ff <(grep -o 'pattern' file1) file2