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?
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
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".
check this link: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html use "^" if it is not starting with 'roll number'
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
grep 4=4 new.txt
. Is it what you want?4
matches all lines that at least contains character4
, no matter of rest