1

I wanted to make a little script to save me some typing, but unfortunately I get no output:

#!/bin/bash grep -Hnr \"$1\" --include \"*cpp\" --include \"*h\" $2 

I played quite a lot with echo and different use of quotes, and convinced myself that line really expands into what I want, but the only way I could actually get any output is with this:

#!/bin/bash GREP="grep -Hnr \"$1\" --include \"*cpp\" --include \"*h\" $2" echo $GREP | bash 

An example usage would be:

srcgrep "dynamic_cast" src 

I've tried this in a simple example directory to rule out anything weird with links, permissions, etc. So, of course I can just use the second, but any idea what's wrong in the first case? Thanks.

$ grep -V grep (GNU grep) 2.5.1 ... $ bash --version GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) ... 
4
  • 1
    what are the expected values of $1 and $2? and could we see some sample lines from the file you're searching? Commented Dec 13, 2012 at 14:50
  • 2
    Have you considered trying ack (betterthangrep.com). If you only want to check source code, then it is a better tool than grep. ack --type=cpp searchterm" would be the command. You might be able to set the --type in an ~/.ackrc or an environment variable to shorten it to ack searchterm. Commented Dec 13, 2012 at 14:51
  • @nullrevolution In my example they would be dynamic_cast and src. Doing the grep by hand with those values works fine. @gpojd I'm aware of that program, but getting things installed is not an entirely pain-free process here. Commented Dec 13, 2012 at 15:02
  • @Chris, there is a standalone program that is easy to install without any elevated user permissions. You can use curl or wget and put it in your home directory. I realize that this still might not help, but thought I'd mention it just in case. betterthangrep.com/install Commented Dec 13, 2012 at 15:27

2 Answers 2

3

Why not just:

#!/bin/bash grep -Hnr "$1" --include "*cpp" --include "*h" $2 

?

Sign up to request clarification or add additional context in comments.

2 Comments

Haha, yes, so the answer is "I'm an idiot". I was pessimistically suspicious it might be. I think I got it wrong at first, then thought I needed the slashes while I broke other bits...
naw, not an idiot. Just going for overly complicated when simple will do. forest v.s. trees, basically.
2

So, GNU or someone's found a way to screw up grep with completely inappropriate options. Awesome. They really should have considered the UNIX philosophy of "Do one thing and do it well". grep is for searching for text in files, it's not for finding files. There's a perfectly good command with a somewhat obvious name for FINDing files.

find "$2" -name '*cpp' -o -name '*h' -exec grep -Hnr "$1" {} \; 

assuming "$2" in your posted example is a directory name instead of a file name as you'd expect grep to work on.

5 Comments

Took me a moment to remember why I stopped using that. It seems to group the -exec inside the -o: find src (-name "*cpp") -o (-name "*h" -exec ...)\;. So it only greps inside *h files, not *cpps. Feels wrong to type the whole exec bit out twice. And using xargs is a bit messy too.
Just found out you can use braces to group the search terms: find src \( -name "*cpp" -o -name "*h" \) -exec grep -Hn "dynamic_cast" {} \; That does make this a good option too.
@Chris thanks for the research on that. I suspect if you ever have to expand your file search criteria you'll be happy you went with the find approach rather than those goofy grep options. Can't believe the GNU guys did that - what's next, joining or pasting files with grep?
You mean like grep -h "^" f1 f2?
That command wouldn't do a join (print common lines) or a paste (print all lines side by side), thankfully. I mean like the non-existent (at least so far!) grep --join -h "^" f1 f2 or grep --paste -h "^" f1 f2.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.