Operating System - HP-UX
1752661 Members
5554 Online
108788 Solutions
New Discussion юеВ

Re: how to search the exact word using grep in hpux

 
SOLVED
Go to solution
Shailendran V Naidu
Frequent Advisor

Re: how to search the exact word using grep in hpux

Try,

grep -c "ze4egi\>" /etc/passwd
James R. Ferguson
Acclaimed Contributor

Re: how to search the exact word using grep in hpux

Hi (again):

The '-w' switch isn't/wasn't available in 10.20 or 11.0.

Since you are searching '/etc/passwd' you might anchor your matches with ":" like:

# grep ":ze4egi:" /etc/passwd

Regards!

...JRF...
mobidyc
Trusted Contributor

Re: how to search the exact word using grep in hpux

Hello,

You need to use regexp.

you can try:
$ toto=daemon
$ egrep "[^[:alnum:]]$toto[^[:alnum:].]|^$toto[^[:alnum:].]|[^[:alnum:]]$toto$" /etc/passwd
daemon:*:1:5::/:/sbin/sh
$ toto=aemon
$ egrep "[^[:alnum:]]$toto[^[:alnum:].]|^$toto[^[:alnum:].]|[^[:alnum:]]$toto$" /etc/passwd
$

Regards,
Cedrick Gaillard
Best regards, Cedrick Gaillard
mobidyc
Trusted Contributor

Re: how to search the exact word using grep in hpux

Hello,

if your egrep does not understand [^[:alnum:]] you can replace it with :
[^a-zA-Z0-9]

Cheers,
Cedrick Gaillard
Best regards, Cedrick Gaillard
Suraj K Sankari
Honored Contributor

Re: how to search the exact word using grep in hpux

Hi,
See the below link there are so many grep examples are there

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_04_02.html

Suraj
Viktor Balogh
Honored Contributor

Re: how to search the exact word using grep in hpux

it makes no sense grepping in /etc/passwd for whole words since all the lines are consisting of a single word
(instead of whitespace there is a : between the "words" - the only exception is the gecos field, it can contain whitespaces too...)

if you want to search for a particular user then either

# grep "^username:"

or use the special command pwget:

# pwget -n username

or

# awk -F: '{ if ( $1 == "username" ) print $0 }' /etc/passwd

this will print the whole line from /etc/passwd.

****
Unix operates with beer.
Viktor Balogh
Honored Contributor

Re: how to search the exact word using grep in hpux

sed works too:

# printf '\nthis is a sample\nfor \
> what can be done\nwith sed\n\n'

this is a sample
for what can be done
with sed

# printf '\nthis is a sample\nfor \
> what can be done\nwith sed\n\n' |
> sed -n '/\s*what\s*/p'
for what can be done
#
****
Unix operates with beer.
Viktor Balogh
Honored Contributor

Re: how to search the exact word using grep in hpux

corrected:

sed -n '/\swhat\s/p'

without the stars it match all the words that has whitespace(s) before and after
****
Unix operates with beer.
Bill Hassell
Honored Contributor

Re: how to search the exact word using grep in hpux

> grep: illegal option -- w

I suspect that your man pages do not mention -w either. The -w option was added to 11.00 or 11.11 as a 'funny' patch, that is, there didn't seem to be a grep patch but it was added with something else. grep is telling you that the option does not exist in the (old) version you are using.

As mentioned, the passwd file doesn't have words -- words being defined as text surrounded by white space. If you are searching the GECOS field (the user's name and related information), you need to isolate GECOS with something like awk and then search for words:

awk -F: '{print $5}' /etc/passwd | grep -w ze4egi

This will only work for a grep that has the -w option.


Bill Hassell, sysadmin