LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to find exact results from grep (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-exact-results-from-grep-4175436853/)

teorxv102 11-12-2012 08:04 PM

how to find exact results from grep
 
My question is if i use a grep to find a statement from /etc/default/password
what coding should i need if /etc/default/passwd is 'passlength=' which is empty right after = because grep always takes the any stuff that matches it like 'passlength=2'.

heres my coding
if grep 'PASSLENGTH=' /etc/default/passwd
then
echo exception Yes
else
echo exception No
fi
Thanks in advance

Diantre 11-12-2012 08:34 PM

Assuming there's an end-of-line right after 'PASSLENGTH=', you can use

Code:

grep 'PASSLENGTH=$' ...

towheedm 11-12-2012 08:44 PM

Please use code tags around your codes. It preserves formatting and makes it easier to read.

Form the regex accordingly. I'm assuming passlength= starts on a new line and has nothing after it, ie:

Code:

cat /etc/default/password
passlength=

Try:
Code:

grep "^passlength=$" /etc/default/password
You may also wish to use the --quiet option to grep to suppress printing of any matching lines if using it a script.

teorxv102 11-12-2012 08:48 PM

Thank you both of you. It works now.

towheedm 11-12-2012 08:51 PM

Looking at your previous thread, you may want to read up on regex's. Here's a start:http://www.grymoire.com/Unix/Regular.html

catkin 11-12-2012 09:14 PM

Her's another link http://etext.lib.virginia.edu/servic...nix/regex.html


All times are GMT -5. The time now is 09:23 PM.