127

I have these:

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

I want to find a exact match of ABB.log.

But when I do:

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

it shows all of the lines.

Can I get what I want using grep?

12 Answers 12

206
grep -Fx ABB.log a.tmp

From the grep man page:

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

Sign up to request clarification or add additional context in comments.

7 Comments

wasn't able to use -F unfortunately.
I'm using grep inside a bash script and this option is better than use a regular expression as suggested in the accepted answer. Because I have some special character inside the variable I'm searching (like .) and I don't have to escape them when using the command.
best answer IMO because it allows special characters without escaping
This is better because it also works with variables.
Notice that the long options don't work with some grep tools like the one on BusyBox
|
118

Simply specify the regexp anchors.

grep '^ABB\.log$' a.tmp

3 Comments

Both anchors (^ and $) are needed.
Nice one! And if I'm using the regex for the matching from a file? "grep -f patterns a.tmp" ??
@green69 Several years late, but you can use sed to add the anchors before passing the patterns to grep: sed -r "s/^(.*)$/^\1$/" patterns.txt | egrep -f - a.tmp
24

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

grep -w "ABB.log " a.tmp

2 Comments

Like this one.. it will return First line match
This requires a space after ABB.log, which is not the general case, i.e it will fail most of the time.
3

Most suggestions will fail if there so much as a single leading or trailing space, which would matter if the file is being edited by hand. This would make it less susceptible in that case:

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

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

while read file
do 
  case $file in
    (ABB.log) printf "%s\n" "$file"
  esac
done < a.tmp

Comments

2

similarly with awk

 awk '/^ABB\.log$/' file

Comments

1

I intend to add some extra explanation regarding the attempts of OP and other answers as well.

You can use John Kugelmans' solution like this too:

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

quoting the string and escaping the dot (.) makes it to not need the -F flag any more.

You need to escape the . (dot) (because it matches any character (not only .) if not escaped) or use the -F flag with grep. -F flag makes it a fixed string (not a regex).

If you don't quote the string, you may need 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:

  1. -x forces to match the whole line.
  2. Answers using a non escaped . without -F flag are wrong.
  3. You can avoid -x switch by wrapping your pattern string with ^ and $. In this case make sure you don't use -F, instead escape the ., because -F will prevent the regex interpretation of ^ and $.


EDIT: (Adding extra explanation in regards of @hakre ):

If you want to match a string starting with -, then you should use -- with grep. Whatever follows -- will be taken as an input (not 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

20 Comments

Did you actually experience that -F was missing in your case? If so, can you please say which case that was?
@hakre Did you read my answer completely? I explained (pretty clearly) why -F won't be needed if the . is escaped properly.
Sure. Sure, I'm just asking: Was -F available when you wrote this? If not, which system were you using? I couldn't find that information in your answer so far, which even now is the case when I re-read it again. So really sure, I read your answer completely at least twice. ;)
@hakre Of course -F was available. ( I did say: "or use -F flag with grep")
Thanks for the insight. Just one littel question remains: If -F is the answer, why bother about escaping? What's your opinion about that?
|
1

This worked well for me when trying to do something similar:

grep -F ABB.log a.tmp

Comments

-1
    $ 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
    $

1 Comment

This will also match files that end with ABB.log and the dots should be escaped.
-2

This is with HPUX, if the content of the files has space between words, use this:

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

1 Comment

The OP wanted to match the whole line, not a space-delimited word within the line.
-2

Works for me:

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

\b indicates/sets boundaries.

Seems to work pretty fast

1 Comment

This also matches any line that contains search_word separated by word boundaries. For example, it would match the line, "foo search_word bar".
-3

I'd prefer:

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

cheers

1 Comment

That will match ABBxlog
-4

I needed this feature, but also wanted to make sure I did not return lines with a prefix before the ABB.log:

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

grep "\WABB.log$" -w a.tmp

1 Comment

This will only match if the leading \W is satisfied, which is any non whitespace character, so xABBxlog.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.