LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-16-2004, 09:28 AM   #1
sharonyiisl
LQ Newbie
 
Registered: Sep 2004
Location: malaysia
Posts: 7

Rep: Reputation: 0
how to use grep command


By default grep command gives the filename and the line contains the search word. What command should i use to display only the filenames?

I have another question. What command should i use to display a count of all of filenames in my current working directory that start with the string "Foo" and end with an odd digit?

Thank you very much if you help me.
 
Old 09-16-2004, 09:33 AM   #2
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
For the first question:

grep -l 'pattern' *

For the second, try something like this:

ls -1 | grep '^Foo.*[13579]' | wc -l

Last edited by dsegel; 09-16-2004 at 09:47 AM.
 
Old 09-16-2004, 10:20 AM   #3
sharonyiisl
LQ Newbie
 
Registered: Sep 2004
Location: malaysia
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you for your help!
 
Old 05-27-2006, 04:16 AM   #4
prospekrisal
LQ Newbie
 
Registered: May 2006
Location: Kuching, Malaysia
Posts: 26

Rep: Reputation: 15
Could you explain the command "ls -1 | grep '^Foo.*[13579]' | wc -l", step by step?
 
Old 05-27-2006, 04:27 AM   #5
prospekrisal
LQ Newbie
 
Registered: May 2006
Location: Kuching, Malaysia
Posts: 26

Rep: Reputation: 15
What is the different between ls -1 and ls?
 
Old 05-27-2006, 06:00 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by prospekrisal
What is the different between ls -1 and ls?
man ls----always check the man pages first.....
 
Old 05-27-2006, 06:07 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by prospekrisal
Could you explain the command "ls -1 | grep '^Foo.*[13579]' | wc -l", step by step?
ls -l (long listing)
piped to grep
grep looks for lines beginning with "Foo" followed by any character, and then one of the characters 1,3,5,7,9

then piped to wc (word count)

To see if I did this right--and to learn much more, got to tldp.org and get "Bash guide for Beginners" by Machtelt Garrels.
 
Old 05-28-2006, 03:46 PM   #8
dsegel
Member
 
Registered: Aug 2004
Location: Davis, California
Distribution: Gentoo, always Gentoo.
Posts: 159

Rep: Reputation: 30
'ls -1' (that's the number one) gives you a listing of all the files in a single column, good for counting. A regular 'ls' will output the listing with spaces between the filenames, but the listing goes across so it's (slightly) harder to count the number of files.

the 'wc -l' command counts lines fed to it, as opposed to just counting words. You could use the regular 'ls' command and pipe the output to 'wc' with no -l option, but it would miscount files that had spaces in their names. If none of your files had spaces then you could use the command like this:

ls | grep '^Foo.*[13579]' | wc

However, the output of 'wc' without the -l option includes the number of lines, bytes, and words, so you'd have to know what to look at to pull out the value you need. The original method is more direct and could be fed to something else that just needed the line count.

Last edited by dsegel; 05-28-2006 at 03:49 PM.
 
  


Reply



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
grep command itz2000 Linux - Newbie 2 09-21-2005 07:06 PM
grep command in c????? alnreddy Linux - Software 1 12-30-2004 01:01 AM
Develop grep command pengui Programming 1 10-16-2004 01:48 AM
Help With GREP Command juliettree Linux - Newbie 3 04-08-2004 08:44 AM
grep command cuss Linux - General 7 02-14-2003 09:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:40 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
Open Source Consulting | Domain Registration