0

I'm tring to grep strings by files. The issue is, grep doesn't do well with lines that have multiple strings (that I know of), which I want to grep individually. Grep may not be the easiest solution here, but it's what I know, so it's what I'm using. In short, I have 2 files I'm using. The first file (file2.txt) that I'm searching is setup as follows:

a c  
d  
f  
g  
b h  
h  

The second file (file1.txt) that I'm using as my search argument is as follows:

a b

I get no return When I search with grep using:

grep -f file1.txt file2.txt

However I would like a return that brings me these lines because "a" and "b" are found in them in file2:

a c
b h

1 Answer 1

0

Probably the simplest thing to do is convert file1.txt to a newline-separated stream, and pass that to grep -f via stdin:

$ tr -s '[:blank:]' '\n' < file1.txt | grep -Ff- file2.txt
a c
b h
1
  • Thanks! This does the trick. Aug 27, 2019 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.