Easy Grep Question

This seems like an easy question, but I can't find an answer already posted.

I want a command to return all of the lines in a file containing exactly a string

I tried

 grep -x "372701" x.txt 

but this did not return anything

I am just trying to search a file for lines which contain exactly a number 1000, but does not return 10000, 11000, 111000, 100000, and so on.

Try:

grep '^1000$' 

But -x should work. Maybe you have some spaces?

No spaces. What if I needed to search exactly for a variable?

for example, x=1000, and I needed a grep statement to only return lines in a file containing exactly x

grep '^1000$'this doesnt work. Do you have any other solution which wil give the desired output

That solution is correct. Although to be safe you should add -F for fixed (literal) string matching, since regular expressions are not needed (in this context, they can only lead to errors and needless computational overhead).

If you are not seeing any output, then there is something else on the line besides 372701. I suggest taking a closer look at your file (perhaps an octal or hex dump) to see what's there. Perhaps there's a carriage return at the end of the line (not uncommon if the data was handled or served by a Windows machine).

If not that, then perhaps you mistyped something.

Regards,
Alister