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.. |
Try
grep ".c" temporary.c |
Hello ajink,
you'll have to grep for .c Code:
grep ".c" temporary.c Code:
grep .c temporary.c Markus |
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 |
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 |
not grep but Ruby
Code:
ruby -ne 'print if /\.c$/' file |
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 |
Quote:
|
It does tend to get overlooked, doesn't it?
And even I keep forgetting that grep -F is aliased to fgrep. :D |
Quote:
|
Quote:
Quote:
Code:
grep '\.c\>' temporary.c |
All times are GMT -5. The time now is 10:14 PM. |