1760364 Members
4427 Online
108893 Solutions
New Discussion юеВ

grep

 
khilari
Regular Advisor

grep

Hi people, okay a command in grep is not working.
i did /etc/passwd | grep
Now, that username was there, but the output that i got was /etc/passwd[14]: mysql:*:102:102::/home/mysql:/sbin/sh: not found. And the list went on like this.
Now, to my understanding, u put in a file name and then concatenate it with grep to find a particular string in that file or what ever is it that u r lookig it in.
So, how come the grep is giving me alist of all the usernames when all i am grepping is a particular and unique username.
13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: grep

shalom,

Try grep username /etc/passwd

You might get better results. You'll certainly get them more efficiently.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Arunvijai_4
Honored Contributor

Re: grep

Hi Khilari,

You should rather use,

# grep -i mysql /etc/passwd

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Peter Godron
Honored Contributor

Re: grep

Khilari,
as a second option :
You may have missed out "cat " in front of the /etc/passwd.
Patrick Wallek
Honored Contributor

Re: grep

If all you did was:

# /etc/passwd | grep

Then no, that will not work. Your example would try to execute the /etc/passwd file, which would not work and would error on each and every line. You got all lines, because 1) they are not valid shell script commands and 2) grep works on 'standard out' file descriptor and everything you saw was going to 'standard error' file descriptor.

You must cat the file and pipe to grep:

# cat /etc/passwd | grep

or you can just use the grep command and specify the file name you are querying at the end of the command, as previous post suggested.

# grep /etc/passwd
Hein van den Heuvel
Honored Contributor

Re: grep

>> Hi people, okay a command in grep is not working.

Hmm... the unix world as we know it would come to a grinding hold if grep was as badly broken as you suggest.

>> i did /etc/passwd | grep

Ah.... you missed a 'cat ' in front of that, or just use the preferred 'grep ' as already replied.

>> output that i got was /etc/passwd[14]: mysql:*:102:102::/home/mysql:/sbin/sh: not found. And the list went on like this.

Seems to me you are actually EXECUTING passwd as a script and indeed 'mysql' would be a command which is not found.

What is the MODE on /etc/passwd
You should not have an x (execute) bit there.

hth,
Hein.
James R. Ferguson
Acclaimed Contributor

Re: grep

Hi:

If you are going to 'grep' usernames in '/etc/passwd' I suggest that you do something like:

# grep "^tftp:" /etc/passwd

That is, the caret (^) anchors the search to the beginning of the line. The colon (:) matches the string at the '/etc/passwd's first field end. This will give much more accurate matching and eliminate spurious matching.

Do *not* spawn a new, unnecessary process by doing :

# cat /etc/passwd | grep ...

That is needless and wasteful. 'grep' takes its input from STDIN or the filename supplied as its last argument.

Regards!

...JRF...
khilari
Regular Advisor

Re: grep

# grep -i mysql /etc/passwd
mysql:*:102:102::/home/mysql:/sbin/sh
# cat /etc/passwd | grep mysql
mysql:*:102:102::/home/mysql:/sbin/sh

Now, y first the grep was before the /etc/passwd and in the other case after it. Yet giving the same output.

Do u huys ever use egrep or fgrep on a regular basis.
James R. Ferguson
Acclaimed Contributor

Re: grep

Hi (again):

If you use the '-E' and '-F' switches with 'grep' you get the functionalty of 'egrep' and 'fgrep' respectively.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: grep


>> Now, y first the grep was before the /etc/passwd and in the other case after it. Yet giving the same output.

That's very basic Unix shell behaviour.

The syntax for grep and many other commands (zip, head, wc,...) is:

command

If no filename for is provided then in used.

In the second case, you did not give am explicit filespace to grep, so it read STDIN which got it's data piped in from the preceding pipe command.

Clear as mud?

Hein.