0

In txt1

S01A1P2 S01A1P5 S01A1P4 

In txt2

data/train/wave/S01A1P3.mfc data/train/wave/S01A1P7.mfc data/train/wave/S01A1P8.mfc data/train/wave/S01A1P1.mfc data/train/wave/S01A1P5.mfc data/train/wave/S01A1P6.mfc data/train/wave/S01A1P2.mfc data/train/wave/S01A1P4.mfc 

Use grep -f txt1 txt2 and result

data/train/wave/S01A1P4.mfc 

But I want this result that find all pattern

data/train/wave/S01A1P5.mfc data/train/wave/S01A1P2.mfc data/train/wave/S01A1P4.mfc 

What can I do?

5
  • Add output of file txt1 txt2 to your question. Commented Aug 24, 2016 at 5:05
  • 2
    grep -f txt1 txt2 works for me, matching 3 lines. Commented Aug 24, 2016 at 5:06
  • txt1 txt2 Commented Aug 24, 2016 at 5:30
  • It works well, On which operating system you are running this ? Commented Aug 24, 2016 at 5:40
  • I'm running on Ubuntu 15.04 Commented Aug 24, 2016 at 5:43

2 Answers 2

3

txt1 contains CRLF line terminators. Try this:

grep -f <(dos2unix <txt1) txt2 
Sign up to request clarification or add additional context in comments.

1 Comment

or use grep -f <(tr -d '\r' <txt1) txt2
0
grep -f txt1 txt2 

Works for me too.

But you can also try to do it the ugly way-

for i in `cat txt1` do grep "$i" txt2 done 

1 Comment

The ugly way doesn't work for me too :( . Where is my problem?