How to make grep just a match if the whole string matches?

I have:

$ cat a.tmp ABB.log ABB.log.122 ABB.log.123 

I wanted to find an exact match for ABB.log.

But when I did

 $ grep -w ABB.log a.tmp ABB.log ABB.log.122 ABB.log.123 

he displays them all.

Can I get what I wanted using grep?

+87
unix shell grep
Jan 17 2018-11-11T00:
source share
12 answers

Just specify regexp bindings.

 grep '^ABB\.log$' a.tmp 
+89
Jan 17 2018-11-11T00:
source share
 grep -Fx ABB.log a.tmp 

On the grep man page:

-F, --fixed-strings
Interpret PATTERN as a (list) of fixed strings
-x, -line-regexp
Select only matches that exactly match the entire string.

+126
Jan 17 2018-11-11T00:
source share

Here is what I do, although using anchors is the best way:

 grep -w "ABB.log " a.tmp 
+24
Mar 13 2018-12-12T00:
source share

similar to awk

  awk '/^ABB\.log$/' file 
+2
Jan 17 '11 at 4:05
source share

Most suggestions will fail if there is only one leading or trailing space, which matters if the file is edited manually. This would make him less susceptible in this case:

 grep '^[[:blank:]]*ABB\.log[[:blank:]]*$' a.tmp 

A simple while-read loop in the shell will do this implicitly:

 while read file do case $file in (ABB.log) printf "%s\n" "$file" esac done < a.tmp 
+2
Mar 15 '13 at 11:32
source share

I intend to add further explanation regarding the OP attempts and other answers.

You can use John Kugelmans solution like this:

 grep -x "ABB\.log" a.tmp 

quoting the line and avoiding the period ( . ), the -F flag is no longer needed.

You need to avoid . (period) (because it matches any character (not only . ) if not escaped) or use the -F flag with grep. The -F flag makes it a fixed string (not a regular expression).

If you don't quote the line, you may need a double backslash to escape the dot ( . ):

 grep -x ABB\\.log a.tmp 




Test:
 $ echo "ABBElog"|grep -x ABB.log ABBElog #matched !!! $ echo "ABBElog"|grep -x "ABB\.log" #returns empty string, no match 




Note:
  • -x forces the entire line.
  • Answers using unshielded . without the -F flag erroneous.
  • You can avoid the -x switch by wrapping the pattern string with ^ and $ . In this case, make sure that you do not use -F , but avoid . , because -F will interfere with the interpretation of the ^ and $ regular expressions.




EDIT: (Adding further explanation regarding @hakre):

If you want to combine a line starting with - then you should use -- with grep. Everything that follows -- will be taken as an input (not an option).

Example:

 echo -f |grep -- "-f" # where grep "-f" will show error echo -f |grep -F -- "-f" # whre grep -F "-f" will show error grep "pat" -- "-file" # grep "pat" "-file" won't work. -file is the filename 
+1
Jul 12 '15 at 14:28
source share

This worked for me when I try to do something like this:

 grep -F ABB.log a.tmp 
+1
Dec 14 '15 at 11:39
source share

Works for me :

 grep "\bsearch_word\b" text_file > output.txt 

\b points / sets bounds.

Seems to work pretty fast

0
May 11 '16 at 18:53
source share
  $ cat venky ABB.log ABB.log.122 ABB.log.123 $ cat venky | grep "ABB.log" | grep -v "ABB.log\." ABB.log $ $ cat venky | grep "ABB.log.122" | grep -v "ABB.log.122\." ABB.log.122 $ 
-one
Mar 15 '13 at 10:10
source share

This is with HPUX, if the contents of the files have a space between words, use this:

egrep "[[:space:]]ABC\.log[[:space:]]" a.tmp

-2
Jul 14 '14 at 7:33
source share

I need this function, but I also wanted to make sure that I did not return prefixed strings before ABB.log:

  • ABB.log
  • ABB.log.122
  • ABB.log.123
  • 123ABB.log

 grep "\WABB.log$" -w a.tmp 
-3
Sep 11 '12 at 17:33
source share

I would prefer:

 str="ABB.log"; grep -E "^${str}$" a.tmp 

amuses

-3
Apr 04 '13 at 8:51
source share



All Articles