Display exact matches only with grep

前端 未结 8 939
半阙折子戏
半阙折子戏 2020-12-09 16:26

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 applic         


        
相关标签:
8条回答
  • 2020-12-09 16:32

    try this:

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

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

    0 讨论(0)
  • 2020-12-09 16:40

    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
    
    0 讨论(0)
  • 2020-12-09 16:43

    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

    0 讨论(0)
  • 2020-12-09 16:44

    ^ marks the beginning of the line and $ marks the end of the line. This will return exact matches of "OK" only.

    (This also works with double quotes if that's your preference.)

    grep '^OK$'
    
    0 讨论(0)
  • 2020-12-09 16:46

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题