LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2008, 11:09 AM   #1
krdan4th
LQ Newbie
 
Registered: Sep 2008
Location: Massachusetts, United States
Distribution: Ubuntu 8.10
Posts: 15

Rep: Reputation: 0
Grep help Please


Hi all
I know this is a Linux forum but i am taking a course in school and we are using UNIX and we are working in the born again shell. and for a lab we have to make a list of some commands. But I am having some trouble with the grep command. I'm not asking for any one to do this for me just asking for some one to point me in the direction of a tutorial that can help me figure out witch commands to use. Or if some one wants to explain to me what commands to use that would so helpful. I know I need to use []* and <> I am just not sure how to use them in the command.

here is what I need to do.
6.) Now to play with grep. Using the file /etc/passwd, write the following greps (the basic form will be “cat /etc/passwd | grep pattern”

a) lines that have “russ”

b) users that use bash

c) users that do not use bash

d) the count of users that use bash

e) The number of users in the /home directory

f) Dead accounts (users with a false shell) and what line number they are on

Thank you so much to helps me out with this and who every does you would seriously be the coolest stranger in the world!!!

Thanks
Kev
 
Old 11-13-2008, 11:16 AM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 234Reputation: 234Reputation: 234
You know, a quick look at the man page for grep would have given you all of these answers:

a) lines that have “russ”

cat /etc/passwd | grep "russ"

b) users that use bash

cat /etc/passwd | grep "bash"

c) users that do not use bash

cat /etc/passwd | grep -v "bash"

d) the count of users that use bash

cat /etc/passwd | grep "bash" | wc -l

e) The number of users in the /home directory

cat /etc/passwd | grep "/home" | wc -l

f) Dead accounts (users with a false shell) and what line number they are on

cat /etc/passwd | grep -v -n "bash"
 
Old 11-13-2008, 11:21 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
1. I don't understand the reason of the syntax "cat /etc/passwd | grep pattern" since grep accept a file name as argument, but if your course notes do that, stick with it.

2. Do a copy of /etc/passwd and work on that (you never know...)

3. All you need is the grep command and its options, so look at man grep and start to figure it out. Take in mind that the syntax of the grep command is
Code:
grep [options] PATTERN [FILE...]
where PATTERN is the word or the pattern to look for inside FILE.

Good luck!
 
Old 11-13-2008, 11:26 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by trickykid View Post
cat /etc/passwd | grep "/home" | wc -l
Hey trickykid, apart the fact he/she could have tried to do the homework by him/herself, don't you think that the usage of the count option of grep would be more correct in this context?
 
Old 11-13-2008, 11:33 AM   #5
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,971
Blog Entries: 1

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by colucix View Post
Hey trickykid, apart the fact he/she could have tried to do the homework by him/herself, don't you think that the usage of the count option of grep would be more correct in this context?
True...somthing like...

Code:
root@host# grep -c home /etc/passwd
krdan4th, you will NEVER learn Linux if you don't do your own homework...

-C
 
Old 11-13-2008, 11:37 AM   #6
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by colucix View Post
Hey trickykid, apart the fact he/she could have tried to do the homework by him/herself, don't you think that the usage of the count option of grep would be more correct in this context?
True, but then again, I went way beyond than I should have.

I did stick to the cat /etc/passwd bit they are stuck to using apparently, IMHO I would have just appended the filename at the end, etc.
 
Old 11-13-2008, 11:51 AM   #7
ritam_bkp
Member
 
Registered: Sep 2008
Location: Calcutta,India
Distribution: Fedora 11, OpenSUSE 11.0
Posts: 116

Rep: Reputation: 17
Talking

Quote:
Originally Posted by trickykid View Post
You know, a quick look at the man page for grep would have given you all of these answers:

a) lines that have “russ”

cat /etc/passwd | grep "russ"

b) users that use bash

cat /etc/passwd | grep "bash"

c) users that do not use bash

cat /etc/passwd | grep -v "bash"

d) the count of users that use bash

cat /etc/passwd | grep "bash" | wc -l

e) The number of users in the /home directory

cat /etc/passwd | grep "/home" | wc -l

f) Dead accounts (users with a false shell) and what line number they are on

cat /etc/passwd | grep -v -n "bash"
that wasof lot of help..
thanx 2 trickykid
 
Old 11-13-2008, 07:54 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Just make sure you actually read the man page and understand it (and trickykid's answers) thoroughly.
 
Old 11-13-2008, 08:10 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
There may also be a "regex" posix manpage that will explain using regular expressions and extended regular expressions.
 
Old 11-14-2008, 01:47 AM   #10
krdan4th
LQ Newbie
 
Registered: Sep 2008
Location: Massachusetts, United States
Distribution: Ubuntu 8.10
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks!!

Thanks a lot Trickykid! That was so much help!! when I was reading the question I was looking at it and was wonder what the hell was I supposed to do. but now that you showed me what to do i now understand what I was supposed to do and feel like a major moron now. Seeing that, that was so easy.

I am a huge noob to the whole Unix and Linux thing and not so great with using the console since windows was all point and click.

I hope to one day get completely away from windows and use just Linux ia m trying out Ubuntu at the moment and like it so far.

Thanks agian!!
Kev
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 08:12 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration