Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When i use the grep command as follows :

grep $rollno=4 new.txt

it returns the records that contain 44 or 4444 or 24,etc as roll no.How do i eliminate this flaw?

share|improve this question

migrated from Oct 21 '12 at 13:19

This question came from our site for professional and enthusiast programmers.

your command will expand to grep 4=4 new.txt. Is it what you want? – kev Oct 21 '12 at 12:20
could you please explain in more details what your are trying to achieve? basically your pattern 4 matches all lines that at least contains character 4, no matter of rest – laika Oct 21 '12 at 14:34
What a weird migration! – Jonathan Leffler Oct 21 '12 at 15:23

If you have GNU grep, then grep -w 4 will look for 'words' that consist of the digit 4 alone.

grep -w 4 <<eof
This line contains 4 all alone.
This line doesn't because of this 44.
This line contains '4' all alone too.
This line does not because 4x4 looks like a word.
share|improve this answer

If your file contains one record per line, you can use

grep -E "^4$"

The -E option tells grep to accept regular expressions, and in the expression ^ stands for "beginning of line" and $ stands for "end of line".

share|improve this answer

check this link: use "^" if it is not starting with 'roll number'

share|improve this answer

Check that 4 is alone:

$ echo "4 24 4aa 44 74 4 9 kk4 09 4 yu4 4 " | tr ' ' '\n' | egrep '\b4\b'
share|improve this answer

You must log in to answer this question.