Skip to content
Advertisement

bash: grep exact matches based on the first column

I have a .txt file like below:

JavaScript

For example, I want to generate a subset with the IDs 4324321_A3 and 9342432 (based on the first column!). I tried the following command to find the exact matches:

JavaScript

But when I use this line, I end up with a dataset like this:

JavaScript

The problem is that the line that matches a part of the ID (9342432_A1) shouldn’t be there. Can anyone help me with this?

I would like to end up with this:

JavaScript

Advertisement

Answer

It matches

JavaScript

because it has 9342432 in the second column.

You need to update the command to make grep check lines starting with those words, that is, use ^word:

JavaScript

To make it more accurate, you can also use -w that matches the full word. This way grep -wE '^4324321_A3|^9342432' file would not match a line like

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement