1

I have 2 files and I want to grep on them

List1

ACY1
RPL3
RPL4

List2

1   ABHD14A-ACY1
2   ACY1
3   RPL34
4   RPL21
5   RPL3
6   RPL41

I tried something like this

grep -Fwf list1.txt list2.txt and got this

1   ABHD14A-ACY1
2   ACY1
5   RPL3

My list1 did not have ABHD14A-ACY1. Is there a way in grep I can do this?

Thanks

2 Answers 2

1

The -w option makes grep match whole words. Unfortunately, ABHD14A-ACY1 matches ACY1 due to the dash. You might want to add a space to the pattern file like

sed 's/^/ /' file1 | grep -Fwf- file2
2   ACY1
5   RPL3
1

The -w switch returns matches that form whole words.

The reason that it returns ABHD14A-ACY1 and not RPL41 is because ACY1 is at the end of the first and second lines whereas RPL4 is neither at the beginning nor end of any lines nor is it followed by a non-word constituent character which means anything other than a letter, number, or underscore. This is covered in the man page for grep.

To test this, if you add a 1 the end of the first line so that it's 1 ABHD14A-ACY11 and remove the 1 from the end of the sixth line so that it's 6 RPL4 then your output will look like this:

2   ACY1
5   RPL3
6   RPL4

There isn't a way to not return the first line with the exact command that you have or with just calling grep on it's own. You'd have to pipe it into sed to remove the first line:

grep -Fwf list1.txt list2.txt | sed '/ABHD14A/d'

Or pipe it into awk to only print what grep returns after the line containing what you don't want to match:

grep -Fwf list1.txt list2.txt | awk 'NR>1'

Or pipe it into grep -v to exclude the first line:

grep -Fwf list1.txt list2.txt | grep -v ^1

There are other ways to do it but those are the easiest and allow you to just pipe what you have already have.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .