• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3421
  • Last Modified:

grep only text files

i have a firectory conatating somany exe , lib and c ,cpp , and asm files . i want to grep for a word ,say "pointer" in  the directory ...but i want to include only c and cpp files in grepping . hw can i do tat ?
0
karana
Asked:
karana
  • 3
  • 2
  • 2
  • +3
1 Solution
 
ozoCommented:
grep pointer *.{c,cpp}
0
 
vikaskhoriaCommented:
do the following..

grep <req_word>  {<pattern1>,<pattern2>...}

This will look for the req_word in the files whose name matches any of the pattern1, pattern2 etc..
0
 
Kerem ERSOYPresidentCommented:
Hi,

You can use find with multiple patterns. This will find both *.c and *.cpp and search the found files:

find . \( -name "*.c" -o -name "*.cpp \)  -exec grep -l "pattern" {} \;

This will list only the filenames with matching contents but not the contents. If you want to display contenets as well please replace the "-l" switch with "-H"

Cheers,
K.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kerem ERSOYPresidentCommented:
also ig you include -I text to grep it will not search binary files.
0
 
Kerem ERSOYPresidentCommented:
Opps sorry my typos I meant:
If you include "-I" siwtch to grep, then it will not match the pattern in binary files but only to files with text contents.
0
 
Gabriel OrozcoSolution ArchitectCommented:
hey, nice proposal Ozo... simple and direct.
0
 
Duncan RoeSoftware DeveloperCommented:
I have a shell script:

07:42:35$ cat fl
#!/bin/sh
j=`basename $0`
case $j in
fl)
  i=""
  ;;
flc)
  i=i
  ;;
*)
  echo bad symlink of fl to $j
  exit 1
  ;;
esac
if [ -z "$1" ];then
  echo "Usage: $j <string> [<grep option>,...]" >&2
  exit 1
else
  k="$1"
  shift
fi
grep -${i}swn "$@" -- $k \
  *.c \
  *.cpp \
  *.cxx \
  *.cc \
  Makefile \
  make-file \
  *.f \
  *.for \
  *.S \
  *.h \
  *.java \
  *.terse \
  *.tcl \
  *.expect \
  *.pl \
  *.i \
  *.l \
  *.y \
  *.sh

For case-independent matching, use flc:

07:43:05$ ls -l flc
lrwxrwxrwx  1 root root 2 2004-09-30 22:12 flc -> fl

This script only matches words (-s option to grep). You can miss that out if you like.

I also have a script to search the current dir and below, either matching words (sfl*) or any match (afl*), or to search current & sibling directories for words(ufl*):

07:49:59$ cat sfl
#!/bin/sh
#set -x

# Runs the show
main()
{
  try_gnu_args
  set_personality || exit 1

# Had to inline the next bit, coz it uses shift & args are local to functions
if [ -z "$1" ];then
  echo "Usage: $j <string> [<grep option>,...]" >&2
  exit 1
else
  k="$1"
  shift
fi

do_the_grep "$@"
}

# Sets fiddly gnu arg iff we have gnu grep
try_gnu_args()
{
  grep --version >/dev/null 2>&1
  [ $? -eq 0 ] && n=-noleaf || n=
}

# Check for good invocation.
# sfl is designed to be symlinked to - which synlink is used determines behaviour.
# list of symlinks:
# lrwxrwxrwx  1 dunc users    3 2004-09-30 22:12 afl -> sfl
# lrwxrwxrwx  1 dunc users    3 2004-09-30 22:12 aflc -> sfl
# lrwxrwxrwx  1 dunc users    3 2004-09-30 22:12 afll -> sfl
# lrwxrwxrwx  1 dunc users    3 2004-09-30 22:12 afllc -> sfl
# -rwxr-xr-x  1 dunc users 1183 2006-05-04 07:59 sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 sflc -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 sfll -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 sfllc -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 ufl -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 uflc -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 ufll -> sfl
# lrwxrwxrwx  1 root root     3 2004-09-30 22:12 ufllc -> sfl
set_personality()
{
  j=`basename $0`
  case $j in
  u*)
    f='.. -maxdepth 2 -mindepth 2'
    w=w
    ;;
  s*)
    f=.
    w=w
    ;;
  a*)
    f=.
    w=""
    ;;
  *)
    echo bad symlink of sfl to $j
    return 1
    ;;
  esac
  case $j in
  ?fl)
    i=''
    ;;
  ?fll)
    i=''
    f="$f -follow"
    ;;
  ?flc)
    i=i
    ;;
  ?fllc)
    i=i
    f="$f -follow"
    ;;
  *)
    echo bad symlink of sfl to $j
    return 1
    ;;
  esac
  return 0
}

# Actually do the grep
do_the_grep()
{
  find $f $n -depth -type f \( \
    -name "*.c" \
    -o -name "*.cpp" \
    -o -name "*.cxx" \
    -o -name "*.cc" \
    -o -name "*.f" \
    -o -name "*.for" \
    -o -name "*.S" \
    -o -name "*.h" \
    -o -name "*.java" \
    -o -name "*.exp" \
    -o -name "*.tcl" \
    -o -name "*.expect" \
    -o -name "*.pl" \
    -o -iname "makefile" \
    -o -name "Kconfig" \
    -o -name "make-file" \
    -o -name "*.i" \
    -o -name "*.l" \
    -o -name "*.lex" \
    -o -name "*.y" \
    -o -name "*.yacc" \
    -o -name "*.sh" \
    \) -print|sed -e 's/ /\\ /g'|xargs -n 32 -r -e grep -${i}s${w}n "$@" -- "$k" /dev/null
}
main "$@"

Hope they are of some help to you
0
 
Gabriel OrozcoSolution ArchitectCommented:
well, for recursivity, grep has -R
for many wors or "patterns", egrep can match regular expressions, which are way more powerful:

egrep -R "match1|othermatch|pointer3"  *.{c,cpp}
0
 
Duncan RoeSoftware DeveloperCommented:
Yes it depends what you want. I usually want to find all occurrences of a variable. Also I need it to work on platforms other than linux, which have non-gnu grep with far less options
0
 
karanaAuthor Commented:
ozo , ur right ,

but for me

 grep pointer *(.c ,.cpp)

dot should be inside ,Not outside the brace .
0
 
karanaAuthor Commented:
but when i use

grep -r  pointer *(.c ,.cpp)

it fails ????!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now