LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-12-2011, 04:58 AM   #1
ajink
LQ Newbie
 
Registered: May 2010
Posts: 11

Rep: Reputation: 0
grep command


Guys... I have a file temporary.c
This file contains foll. data:
example1.c
example2.c
example3.c
Now m using foll command to find all lines having .c pattern

grep "*.c" temporary.c
but it is not working.. Plz can someone help..

Last edited by ajink; 04-12-2011 at 05:01 AM.
 
Old 04-12-2011, 05:03 AM   #2
agambier
LQ Newbie
 
Registered: Nov 2010
Location: Annecy, France
Distribution: Fedora 13
Posts: 22

Rep: Reputation: 2
Try

grep ".c" temporary.c
 
Old 04-12-2011, 05:05 AM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello ajink,

you'll have to grep for .c
Code:
grep ".c" temporary.c
you don't need the quotes
Code:
grep .c temporary.c
yields the same output

Markus
 
Old 04-12-2011, 05:32 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
Markus, we need to remember that the dot in a pattern (as in regular expressions) means any single character. Maybe here we want to match a literal dot and to achieve that we have to escape it or enclose it in a character list. In any case, better to put single quotes around the pattern to prevent unwanted shell substitutions when dealing with special characters:
Code:
grep '\.c' temporary.c
grep '[.]c' temporary.c
Just my
 
1 members found this post helpful.
Old 04-12-2011, 05:38 AM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Hello colucix,

I was thinking about the dot in regular expressions, but I wasn't aware that grep by default (as in this case) evaluates the '.' as the regexp for "any single character".

Thanks for pointing that out.

Markus
 
Old 04-12-2011, 05:50 AM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
not grep but Ruby

Code:
ruby -ne 'print if /\.c$/' file
 
Old 04-12-2011, 06:35 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The real secret here is not in grep itself, but in the regular expression pattern matching, as has been pointed out above. The grep man page has a decent starter explanation of them, so read that first. After that, get on the net and google yourself a good regex tutorial. You'll be glad you did.

But after saying that, there is a grep-specific solution too. Use -F to force it to search for fixed strings.
Code:
grep -F ".c" file
 
2 members found this post helpful.
Old 04-12-2011, 07:25 AM   #8
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 David the H. View Post
But after saying that, there is a grep-specific solution too. Use -F to force it to search for fixed strings.
Good shot! I always forget about the -F option!
 
Old 04-12-2011, 07:36 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
It does tend to get overlooked, doesn't it?

And even I keep forgetting that grep -F is aliased to fgrep.
 
Old 04-12-2011, 08:18 AM   #10
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Quote:
Originally Posted by David the H. View Post
Code:
grep -F ".c" file
of course, in corner cases, this will not be the solution if other parts of the string contains ".c" but i digress...
 
Old 04-12-2011, 08:52 AM   #11
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Quote:
Originally Posted by David the H. View Post
...After that, get on the net and google yourself a good regex tutorial. You'll be glad you did.
I agree, one of the most important books about computers which I know is "Mastering Regular Expressions" by Jeffrey E.F. Friedl http://regex.info/
Quote:
Originally Posted by kurumi
of course, in corner cases, this will not be the solution if other parts of the string contains ".c" but i digress...
you may instead search for a "c" followed by any empty string
Code:
grep '\.c\>' temporary.c
Markus
 
1 members found this post helpful.
  


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
How to pass the result of a command to another command (like grep) desb01 Programming 4 06-25-2009 01:09 PM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 05:16 AM
grep command karimasif Linux - Newbie 2 09-05-2007 12:54 PM
grep command sgarci Linux - General 3 08-28-2006 08:13 AM
Help With GREP Command juliettree Linux - Newbie 3 04-08-2004 09:44 AM


All times are GMT -5. The time now is 10:49 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