Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display exact matches only with grep

Tags:

unix

control-m

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 
like image 341
heinistic Avatar asked Oct 24 '13 20:10

heinistic


People also ask

How do you only grep an exact word?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

How do I grep for a specific string?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

What is I flag in grep?

grep -i “paTTern” file. The -i flag makes sure that grep performs a case-insensitive search. It displays all the lines in file. txt that contain the word paTTern , regardless of the alphabetical case as a string or sub-string.


1 Answers

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 
like image 97
lurker Avatar answered Sep 19 '22 15:09

lurker