Reputation: 2136
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?
Upvotes: 126
Views: 255181
Reputation: 11588
Works for me:
grep "\bsearch_word\b" text_file > output.txt
\b
indicates/sets boundaries.
Seems to work pretty fast
Upvotes: -2
Reputation: 1508
I needed this feature, but also wanted to make sure I did not return lines with a prefix before the ABB.log:
grep "\WABB.log$" -w a.tmp
Upvotes: -4
Reputation: 191
This worked well for me when trying to do something similar:
grep -F ABB.log a.tmp
Upvotes: 1
Reputation: 362157
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.
Upvotes: 203
Reputation: 22448
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
$ echo "ABBElog"|grep -x ABB.log
ABBElog #matched !!!
$ echo "ABBElog"|grep -x "ABB\.log"
#returns empty string, no match
-x
forces to match the whole line..
without -F
flag are wrong.-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 $
.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
Upvotes: 1
Reputation: 21
This is with HPUX, if the content of the files has space between words, use this:
egrep "[[:space:]]ABC\.log[[:space:]]" a.tmp
Upvotes: -2
Reputation: 9946
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
Upvotes: 3
Reputation: 7
$ 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
$
Upvotes: -1
Reputation: 349
Here is what I do, though using anchors is the best way:
grep -w "ABB.log " a.tmp
Upvotes: 24