41

How can I display all jobs that ended OK only?

When I try the command below, it shows both OK and NOTOK since both have "OK"

ctmpsm -listall application | grep OK
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
heinistic
  • 731
  • 2
  • 8
  • 16

9 Answers9

59

You need a more specific expression. Try grep " OK$" or grep "[0-9]* OK". You want to choose a pattern that matches what you want, but won't match what you don't want. That pattern will depend upon what your whole file contents might look like.

You can also do: grep -w "OK" which will only match a whole word "OK", such as "1 OK" but won't match "1OK" or "OKFINE".

$ cat test.txt | grep -w "OK"
1 OK
2 OK
4 OK
lurker
  • 56,987
  • 9
  • 69
  • 103
  • This worked fine.. but now I'm trying it with other commands such as `ctmpsm -listall application | grep -w "ABCD123"` there are lines display `ABCD123` and `ABCD123@` but I only want `ABCD123` – heinistic Oct 24 '13 at 20:56
  • What *is* allowed after `ABCD123`? Just end of line? Space? Tab? Possibly any of those? – lurker Oct 24 '13 at 21:32
  • 1
    @aguas if there's nothing after it in the file (it's at the end of line), then you can use `grep ".* ABCD123$"` or just `grep " ABCD123$"`. If it is by itself on a line, then `grep "^\s*ABCD123\s*$"`. – lurker Oct 24 '13 at 21:46
22

This may work for you

grep -E '(^|\s)OK($|\s)'
iruvar
  • 22,736
  • 7
  • 53
  • 82
  • 1
    Can you explain the regular expressions here? – Zypps987 Sep 07 '17 at 20:56
  • 1
    `(^|\s)` means beginning of line or space. `($|\s)` means end of line or space. So this would match "OK" as well as " OK " or " OK" or "OK ". – Brad Oct 19 '19 at 16:16
6

This worked for me:

grep  "\bsearch_word\b"  text_file > output.txt  ## \b indicates boundaries. This is much faster.

or,

grep -w "search_word" text_file > output.txt
Surya
  • 11,002
  • 4
  • 57
  • 39
4

You can use the -v switch to exclude strings.

This will grab any line ending in OK and then remove any line that includes NOT:

(If OK doesn't appear at the end, just remove the $.)

cat test.txt | grep 'OK$' | grep -v 'NOT'
Brad
  • 722
  • 2
  • 8
  • 24
  • 1
    Thanks! I was able to use this exact thing to test if my system has memcache installed. When I tried with grep it returned memcache but also memcached so your solution worked great! php-memcache=$(php -m | grep 'memcache$' | grep -v 'NOT') – Robert Saylor May 20 '21 at 16:20
  • You want to avoid the [useless use of `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) though. – tripleee Nov 18 '22 at 08:16
2

Try this:

Alex Misuno@hp4530s ~
$ cat test.txt
1 OK
2 OK
3 NOTOK
4 OK
5 NOTOK
Alex Misuno@hp4530s ~
$ cat test.txt | grep ".* OK$"
1 OK
2 OK
4 OK
plsgogame
  • 1,334
  • 15
  • 28
1

Try the below command, because it works perfectly:

grep -ow "yourstring"

crosscheck:-

Remove the instance of word from file, then re-execute this command and it should display empty result.

enb081
  • 3,831
  • 11
  • 43
  • 66
  • Downvote: the `-o` is not pertinent and the `-w` is already provided in the highest-voted answer. – tripleee Apr 08 '15 at 11:01
  • I disagree this actually provided a better result. When I used the search "xml" grep returned nothing past "xml" but it returned things that started before xml. Like "libxml". When I used this example it returned the exact match. php -m | grep -ow "xml" – Robert Saylor May 20 '21 at 16:36
0

try this:

grep -P '^(tomcat!?)' tst1.txt

It will search for specific word in txt file. Here we are trying to search word tomcat

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
  • 1
    Hi welcome to Stack Overflow. Please edit your answer so it reflects the question asked, that is how to distiguish OK from NOTOK. Thanks – Spangen Nov 02 '17 at 08:55
0

Recently I came across an issue in grep. I was trying to match the pattern x.y.z and grep returned x.y-z.Using some regular expression we may can overcome this, but with grep whole word matching did not help. Since the script I was writing is a generic one, I cannot restrict search for a specific way as in like x.y.z or x.y-z ..

Quick way I figured is to run a grep and then a condition check var="x.y.z" var1=grep -o x.y.z file.txt if [ $var1 == $var ] echo "Pattern match exact" else echo "Pattern does not match exact" fi

https://linuxacatalyst.blogspot.com/2019/12/grep-pattern-matching-issues.html

-2

Search just "ok" word

grep "^ok$"

It means start with "o" and end with "k" and it must contain 2 characters, just it.

  • Do you know that `ctmpsm -listall application` outputs just the result on a line? I'm guessing the OP wants to find "ok" as a word, but print the whole line with other information too. (Which is easy as well; just use `grep -w`.) – tripleee Nov 18 '22 at 08:16