Reputation: 1309
I have a file as below, I want to grep for lines having .c
or .cpp
extension. I have tried using cat file|grep ".c"
grep but I am getting all types of extensions as output. Please shed some light on this. Thanks in advance.
file contents are below:
/dir/a/b/cds/main.c
/dir/a/f/cmdss/file.cpp
/dir/a/b/cds/main.h
/dir/a/f/cmdss/file.hpp
/dir/a/b/cdys/main_abc.c
/dir/a/f/cmfs/file_123.cpp
Upvotes: 4
Views: 8417
Reputation: 185
Also you can use --include
parameter like below
grep --include \*.hpp --include \*.cpp your_search_pattern
Upvotes: 0
Reputation: 4462
$ grep -E '\.cp{2}?' testfile1
/dir/a/b/cds/main.c
/dir/a/f/cmdss/file.cpp
/dir/a/b/cdys/main_abc.c
/dir/a/f/cmfs/file_123.cpp
$
May be this variant will useful. Here p{2}
mean 'symbol p meet 2 times after symbol c'
Upvotes: 0
Reputation: 28920
The Android framework defines a bash function extensions named cgrep
, it goes recursively in the project directory, and it's much faster than using grep -r
.
Usage:
cgrep <expession to find>
it greps only C/C++ header and source files.
function cgrep()
{
find . -name .repo -prune -o -name .git -prune -o -type f \( -name '*.c ' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 gre p --color -n "$@"
}
You can paste this in you .bashrc
file, or use the inline directly in shell.
Upvotes: -1
Reputation:
grep
supports regular expressions.
$ grep -E '\.(c|cpp)$' input
-E
means 'Interpret PATTERN as an extended regular expression'\.
means a dot .
()
is a groupc|cpp
is an alternative$
is the lineendUpvotes: 13