2

I have a file named test.txt test file has the following content of just 4 lines in it:

d
dxx
xxd
xxdxx

when I try to use the following grep command on ununtu, it is giving me no output

$ grep 'd[^x]' test.txt

whereas I expect output to be:

d
xxd

Could any please explain me the reason for such behaviour

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
ABHINAV MEHTA
  • 53
  • 1
  • 6
  • 1
    Do `grep -P 'd([^x]|$)'` this will work even if there is no char after `d` (`-P` is for Perl regexes). Your expression expects a char not-x after d. – Déjà vu Oct 17 '22 at 23:01
  • 1
    This might help: [The Stack Overflow Regular Expressions FAQ](https://stackoverflow.com/a/22944075/3776858) – Cyrus Oct 17 '22 at 23:12

2 Answers2

0

What I would do:

$ grep 'd$' file
d
xxd

Your solution doesn't work, because after d, there's nothing but end of line: $

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

I can solve that issue in 2 ways:

$grep 'd$' test.txt
d
xxd

$grep 'd\>' test.txt
d
xxd

*** But, Can anyone please explain why the following is not working***

$grep 'd[^x]' test.txt
no output
ABHINAV MEHTA
  • 53
  • 1
  • 6