Find all matched pattern with grep

2019-03-03 10:54发布

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?

标签: bash shell grep
2条回答
Explosion°爆炸
2楼-- · 2019-03-03 11:13
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
查看更多
冷血范
3楼-- · 2019-03-03 11:22

txt1 contains CRLF line terminators. Try this:

grep -f <(dos2unix <txt1) txt2
查看更多
登录 后发表回答