2

I'm trying to grab only matching lines from a file using grep. The problem is that, when for example I use the following expression:

grep -w AFFX-KIT-000088 textfile

it will return:

AFFX-KIT-000088-A
AFFX-KIT-000088

However, I only want the exactly matching expression AFFX-KIT-000088. I have already trying -F -E and quoting. The textfile where I need to grep the lines is very big and I can not modify it (this also means I need an answer that doesn't involve many steps as it would take forever to process).

Note:

The file is actually very big and has many columns (not only the one I'm using for grep).

4
grep -x 'AFFX-KIT-000088' file

The -x forces a match of a complete line.

You may also want to add -F as you are matching with a string, not a regular expression. This would possibly speed up the operation.

The -w option does not work here as - is not a "word character". The substring AFFX-KIT-000088 in AFFX-KIT-000088-A is therefore a complete word.

"Word characters" are characters matching [[:alnum:]_], i.e. alphanumerical characters and underscore.


In multi-column data, assuming tab-delimited fields and matching in column 3:

awk -F '\t' '$3 == "AFFX-KIT-000088"' file

That is, compare the 3rd field to a specific string.

When a condition like this lacks an associated action, the default action is to print the whole input record if the condition is true. If you just want to print the field's data:

awk -F '\t' '$3 == "AFFX-KIT-000088" { print $3 }' file
  • 1
    Hi, I didn't menction because I thought I made it obvious by menctioning the file was very big, but the file has many columns (not only the one I'm using for grep), so I can not use -x. I have also tried -F but it doesn't seem to work. I'm, sure that the problem comes from - not being a word character, but the solution requires more informatic knowledge that I probably have have. – Pablo García González Jun 25 at 9:08
  • 1
    @PabloGarcíaGonzález Then you'd better update the question, or you'll get faulty answers like mine. – Kusalananda Jun 25 at 9:09
  • 1
    I will, thanks for your answer anyway :) – Pablo García González Jun 25 at 9:11
  • Okay, awk does the job perfectly fine. Thank you so much! – Pablo García González Jun 25 at 13:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.