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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try one of:
grep -w "deiauk" textfile grep "<deiauk>" textfile
Method 2
Try this with GNU grep and mark word boundaries with b:
grep "bdeiaukb" file
Output:
deiauk 1611516 afsdf 765
See: http://www.regular-expressions.info/wordboundaries.html
Method 3
If your grep supports -P (PCRE), you can do:
$ grep -P '(^|s)Kdeiauk(?=s|$)' file.txt deiauk 1611516 afsdf 765 deiauk 1611516 afsdf ddfgfgd
Method 4
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
Method 5
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 $.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0