LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Recursive grep (https://www.linuxquestions.org/questions/linux-general-1/recursive-grep-99330/)

jimieee 10-02-2003 03:58 AM

Recursive grep
 
Hi

I'm trying to search for perl files that contain the string "Courses" and write an output to a text file containing filename and line where the text occurs.

Doing this: grep Courses | find webct-3.8.0.27 -iname "*.pl"

seems to find the right files, but it just stops after a little while. My question is how can I a) stop it from crashing b) get the other details to be written to a text file.

Any help much appreciated :)

~James~
:newbie:

pavgust 10-02-2003 05:54 AM

Am I being daft, or should the command be the other way round? i.e. don't you need something like

find webct-3.8.0.27 -iname "*.pl" | grep Courses > OutputFile

?

I may be mis-understanding your problem. :D

lugoteehalt 10-02-2003 06:13 AM

You might try:

locate -r '\.pl$' | xargs grep 'Courses' > myfile.txt

-r says expect a regular expression; xargs tells grep to use what locate gives it as arguments, i.e. do not simply look at the strings for Courses. locate may hide some files if you are not root.

yapp 10-02-2003 06:40 AM

"grep -R" :D seams recurse enough for me ;)

and if you want to try some real exclusive commands:

Code:

find . -iname "*.whatever" | while
 read filename
do
 
  .. some commands with $filename

done


yapp 10-02-2003 06:46 AM

Re: Recursive grep
 
Quote:

Originally posted by jimieee
a) stop it from crashing b) get the other details to be written to a text file.
crashing? Maybe "grep" is just a little buzzy.. You didn't specify any file names, so it's waiting for you to type them. Your machine hasn't crashed. it's just waiting for input. Press Ctrl+D to send the end-of-file marker, or press Ctrl+C to terminate the program.

If you use something like "find ... | grep " the output of 'find' will be passed to grep as if you've typed it yourself, and you can use "command < input.txt" as well.

"command > file.txt" redirects the output to a file, and "command >> file.txt" adds the output to the end of the file.

jimieee 10-06-2003 10:13 AM

Thanks guys, I finally found my perfect command:

find webct-3.8.0.27/ -iname *.pl | xargs grep -i courses > Output.txt

As you can see I've used some of the ideas and suggestions that just about all of you made and it was fun to learn a little more about those linux commands :) I would have said thanks sooner, but I've had a little bit of distraction come up on the side of this, so have only just got back around to sorting it out.

Thanks!


All times are GMT -5. The time now is 01:59 AM.