129

I have a text file:

deiauk 1611516 afsdf 765
minkra 18415151 asdsf 4152
linkra sfsfdsfs sdfss 4555
deiauk1 sdfsfdsfs 1561 51
deiauk2 115151 5454 4
deiauk 1611516 afsdf ddfgfgd
luktol1 4545 4 9
luktol 1

and I want to match exactly deiauk. When I do this:

grep "deiauk" file.txt

I get this result:

deiauk 1611516 afsdf 765
deiauk1 sdfsfdsfs 1561 51
deiauk2 115151 5454 4

but I only need this:

deiauk 1611516 afsdf 765
deiauk 1611516 afsdf ddfgfgd

I know there's a -w option, but then my string has to mach whole line.

user3334375
  • 1,745
  • 6
  • 16
  • 16
  • 12
    Have you actually tried `grep -w`? (That option is exactly for that purpose, and it works for me.) - Note: option `-x` matches the whole line. – Janis Jun 01 '15 at 21:53
  • 1
    *"I want to match exactly `deiauk`* / *"I only need this: `deiauk 1611516 afsdf 765`"* - which do you need? – alex Nov 16 '17 at 20:26

5 Answers5

228

Try one of:

grep -w "deiauk" textfile

grep "\<deiauk\>" textfile
Janis
  • 14,014
  • 3
  • 25
  • 42
  • 6
    If you have a dash (**-**) at the end of the string this script will bring it as a result, which was not expected. – Evis Nov 11 '16 at 14:28
  • 1
    Correct @Evert : Words include only alpha chars, digits and underscores, so if you have abbreviations or other items hyphenated, this does not work. – ingyhere Jan 31 '17 at 21:10
  • @Cyrus I learnt the second one while using vi/vim `*` command on words. – KRoy Feb 13 '18 at 19:35
  • It does not work for any special character it have for example `org.apache.avro avro` greped with `org.apache.avro avro+mapred` (tried with *) – Vishrant Mar 28 '19 at 01:30
  • [Use `-wE`](https://unix.stackexchange.com/a/479029/209677) for exact match of multiple words (boolean OR) – Pablo A Sep 11 '20 at 07:44
  • 5
    @Evis Use `grep -w "deiauk$" textfile` instead. – ceremcem Dec 28 '20 at 22:41
  • Thank you @Evis! This is the only solution that works for my problem where I am searching for "skygrid_20-35/mosaics/field_14" and want to exclude "skygrid_20-35/mosaics/field_14.1" – Guillaume Belanger Jun 03 '21 at 14:59
  • @Janis is the second option a gnu feature? That is not working for me in AIX. – cokedude Jun 04 '21 at 14:27
  • first option worked fine for me. – neelmeg Jul 04 '22 at 15:29
27

Try this with GNU grep and mark word boundaries with \b:

grep "\bdeiauk\b" file

Output:

deiauk 1611516 afsdf 765

See: http://www.regular-expressions.info/wordboundaries.html

Cyrus
  • 12,059
  • 3
  • 29
  • 53
15

If your grep supports -P (PCRE), you can do:

$ grep -P '(^|\s)\Kdeiauk(?=\s|$)' file.txt 
deiauk 1611516 afsdf 765
deiauk 1611516 afsdf ddfgfgd
heemayl
  • 54,820
  • 8
  • 124
  • 141
7

Depending on your real data, you could look for the word followed by a space:

grep 'deiauk ' file.txt 

If you know it has to be at the start of the line, check for it:

grep '^deiauk ' file.txt 
Volker Siegel
  • 16,983
  • 5
  • 52
  • 79
  • 1
    Sadly, all of the answers other than this are incorrect. – Shatu Mar 14 '17 at 22:43
  • 1
    @Shatu Thanks! So let's see how long it takes for it to "bubble up to the top"... I'm curious because I like to add answers to old questions... I think it is assumed it does, but I doubt it. It would be just nice for me, but actually useful for the readers. To make this a good example case, could I ask you to write a comment summarizing what the other answers are missing? – Volker Siegel Mar 14 '17 at 22:53
  • 1
    (1) Congratulations on reaching 10K rep. You now have the privilege to see that this answer was given before, and was deleted. (2) It’s always better to answer the question as broadly as possible, based on *what is said,* and not give an answer that works just for the sample data. It appears, from the example data in the question, that the columns are separated by spaces — but that’s not specified. All the other answers will also work for tab-separated columns. (3) You avoided the fatal flaw in tachomi’s (deleted) answer by adding the `^` — but all the other answers work … (Cont’d) – G-Man Says 'Reinstate Monica' Jul 30 '18 at 19:57
  • (Cont’d) …  if the string appears in a field other than the first one.  (4) Also, all the other answers work if ‘‘deiauk’’ is the last field (i.e., there’s nothing after it). – G-Man Says 'Reinstate Monica' Jul 30 '18 at 19:57
  • This answer is not only correct but also readable. And probably the most portable across platforms. – Vorac Jul 22 '21 at 03:33
3

I found -x worked for me.

Example
$ grep -inx -d skip 'favicon.ico' *
test.txt:1:favicon.ico
Grep Manual
-x, --line-regexp
              Select  only  those  matches  that  exactly  match the whole line.  For a regular expression pattern, this is like
              parenthesizing the pattern and then surrounding it with ^ and $.
Tony Barganski
  • 370
  • 4
  • 9
  • For 1:1 matching the whole line which contains hyphens, ``-x`` the only option. For example ``grep -w "abc" <<<"abc-hac101-bb0"`` will match but ``grep -x "abc" <<<"abc-hac101-bb0"`` will not – Invisible999 Nov 11 '20 at 19:44
  • -1 `grep -x` searches for the entire line, so `grep -x deiauk` will not pick up the required line `deiauk 1611516 afsdf 765` only grep -x 'deiauk 1611516 afsdf 765' will do that. The original question was "and I want to match exactly deiauk" which you have not done. – geedoubleya Apr 28 '21 at 12:21