LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep command (https://www.linuxquestions.org/questions/linux-newbie-8/grep-command-874528/)

ajink 04-12-2011 03:58 AM

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..

agambier 04-12-2011 04:03 AM

Try

grep ".c" temporary.c

markush 04-12-2011 04:05 AM

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

colucix 04-12-2011 04:32 AM

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 :twocents:

markush 04-12-2011 04:38 AM

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

kurumi 04-12-2011 04:50 AM

not grep but Ruby

Code:

ruby -ne 'print if /\.c$/' file

David the H. 04-12-2011 05:35 AM

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

colucix 04-12-2011 06:25 AM

Quote:

Originally Posted by David the H. (Post 4322058)
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! :)

David the H. 04-12-2011 06:36 AM

It does tend to get overlooked, doesn't it?

And even I keep forgetting that grep -F is aliased to fgrep. :D

kurumi 04-12-2011 07:18 AM

Quote:

Originally Posted by David the H. (Post 4322058)
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...

markush 04-12-2011 07:52 AM

Quote:

Originally Posted by David the H. (Post 4322058)
...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


All times are GMT -5. The time now is 10:14 PM.