Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep function to find exact word

Status
Not open for further replies.

vti

Technical User
Feb 26, 2001
189
TR
hi i need to use a grep fuction to find exact word i am looking for.
For example :
I am greping for a user john
When i grep for that the result is

john
johnb
johnc
fdjohnf

Is there a way to find only john in list.

Thanks for any help.

 
Put quotes and spaces around the word to grep for
eg
grep " john " filename(s)

Cheers Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi
have you tried with your way,i have tied but couldn't get the result i want.
 
If you can identify the other johns, then try
cat filename(s)|grep john |grep -v johnd|grep -v johnc
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 

Hi dickiebird ;
Sorry about my explanation,i think i didn't explain what exactly i want.

Here is my script i want to run;

if [ $(who |grep -c "^$USER") -gt 1 ]
then
echo '------------------------------'
echo '------------------------------'
echo 'You can not open more than one session'
echo '------------------------------'
echo '------------------------------'
kill -KILL 0
fi

This script works for find more than one sessions and don't let them login.
But when script is run for greping ,grep find also diferent users and kill their sessions as well.But if i can find exact user name it's gonna work as i want and gonna kill only a user who attemps for second session.
 
I think you'll only accomplish this if you have users with unique names - so change user john to johnz and joe to joef etc etc
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 

Do you realy need grep?

for x in $(who -q)
do
if [ $x = $(whoami) ] ;then
echo $x 'You can not open more than one session'
echo I kill you.
sleep 1
kill -KILL 0
break
fi
done

regards gregor Gregor.Weertman@mailcity.com
 
The real question is "what are your delimiters, i.e, what character immediately precedes 'J' and succeeds 'n'?". I suspect that the space didn't work because the character was in fact a tab. If you have a POSIX version of grep then the expression would be "[:blank:]John[:blank:]" or more broadly "[^[:alnum:]]John[^[:alnum:]]". If you don't have a POSIX version then it is "[^0-9A-Za-z]John[^0-9A-Za-z]".

Imagineer
 
In HPUX there is a -x option.
(eXact) Matches are recognized only when the
entire input line matches the fixed string or
regular expression.

I know Sun has such option.


Regards Gregor.



Gregor.Weertman@mailcity.com
 
Works with AIX too : -
who|cut -f1 -d" "|grep -x john
You learn something every day !
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Many thanks ,

it works well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top