How to search file for matching whole lines?

Posted by WilliamKF on Super User See other posts from Super User or by WilliamKF
Published on 2012-09-12T18:57:22Z Indexed on 2012/09/13 9:41 UTC
Read the original article Hit count: 199

Filed under:
|
|
|

I have a command which sends to stdout a series of numbers, each on a new line. I need to determine whether a particular number exists in the list. The match needs to be exact, not a subset. For example, a simple way to approach this that does not work would be to do:

/run/command/outputing/numbers | grep -c <numberToSearch>

My this gives a false positive on the following list when searching for '456':

1234567
98765
23
1771

If the count is non-zero, a match was found or if it was zero, the number is not in the list.

The issue with this is that the numberToSearch could match a subsequence of numbers on a line, instead I only want hits on the whole line. I looked at the man page for grep and did not see any way to only match whole lines. Is there a way to do this, or would I be better off using awk or sed or some other tool instead? I need a binary answer as to whether the number being search for is present or not.

© Super User or respective owner

Related posts about grep

Related posts about sed