Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

When i use the grep command as follows :

rollno=4
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 stackoverflow.com 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
1  
What a weird migration! –  Jonathan Leffler Oct 21 '12 at 15:23
add comment

4 Answers

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.
eof
share|improve this answer
add comment

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
add comment

check this link: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html use "^" if it is not starting with 'roll number'

share|improve this answer
add comment

Check that 4 is alone:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.