Search code examples
linuxbashgrep

Grep match only the exact string in the line


I'm trying to grep for the word OK from the output of the below command

valtool --validate <filename> which throws below outputs

If success --> <filename>: layers signatures val OK & If failure--> <filename>: layers signatures val NOT OK

log='/home/files/rapid.so: layers signatures NOT OK'
~$ echo $log
/home/files/rapid.so: layers signatures NOT OK

~$ echo $log |grep -w 'OK' ;echo $?
/home/files/rapid.so: layers signatures NOT OK
0

Ideally, it should return 1 as per the requirement. I tried other options suggested on this forum but nothing helped. I'm looking to get a return value of 0 if only OK is matched, else 1.

bash --version
GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)

Solution

  • So invert NOT OK.

    ! grep "NOT OK$" <<<"$log"; echo $?