0

I have a file (X.txt) with a list of unique entries. I would like to use this file to grep from another File Y.txt which contain more than one entries for single pattern of X.txt.

X.txt contains:

A.0  
B.0
C.1
D.1

Y.txt contains:

A.0  10
A.0  11
A.1  10
B.0  18
C.1  5
D.1  19
D.1  36
D.2  51

Desired Output:

A.0  10
A.0  11
B.0  18
C.1  5
D.1  19
D.1  36

If they are unique simply using grep -w -f X.txt Y.txt works. But could someone help me how to modify grep for the desired output or using awk.

  • 1
    grep -f X.txt Y.txt should give your desired result. – souravc Feb 24 '15 at 18:02
1

Looks like grep -w does not pick up the A.0 lines because the file X.txt has trailing spaces on that line (breaking the word boundary requirement of -w). Try the "fixed string patterns" flag, -F

grep -Ff X.txt Y.txt

This also works because the files are sorted:

join X.txt Y.txt

Your Answer

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