File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes grep question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "grep question" Watch "grep question" New topic
Author

grep question

vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
I have a target directory in which I need to search for matches for a word only in files which end with extension .jsp.The files may be any number of levels down under the target directory.


For example

etc... My search should look for the word 'isBabyBornYet' in .jsp files but leave java files in the above structure.


I tried this

grep -R 'isBabyBornYet' *.jsp

but it says..

no such file *.jsp.


Thanks,

Vasu
[ January 17, 2005: Message edited by: vasu maj ]

What a wonderful world!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

grep -R 'isBabyBornYet' *.jsp


That works for me. Have you CD'd to a directory that has jsp files, or folders containing jsps?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
I wanted to execute the command from the target directory the intention being to search all the jsp files in the folders and subfolders under it and search for the word.

Thanks,

vasu
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes,
And the command you've posted does just that for me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

*.jsp only matches the jsp files in the current directory, of course. Remember that under UNIX, it's the shell that expands wildcards, not individual programs like grep. If you need to generate a list of files, then you can use any tool in the UNIX arsenal. "find" is the first one I generall reach for:

grep isBabyBornYet `find . -name '*.jsp'`

Note those backticks (`) around the find command; that executes the enclosed command, and inserts the result into the grep command line.


[Jess in Action][AskingGoodQuestions]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Ahh, good point..
All my JSPs were in the current directory so I didn't notice that the recurse wasn't working.

Sorry about that...
vasu maj
Ranch Hand

Joined: Jul 12, 2001
Posts: 395
No probs Ben. Thanks anyway.

Thaks Ernest. How are you and what is latest on jess? (I was one of the winners of your book when it was featured here).

By the way if I gave a command and it is thinking, how do I know if it is working on it or is simply not returing and not doing anything?


Thanks,
Vasu
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by vasu maj:
what is latest on jess?



Charlemagne (Jess 7) is getting closer; the Eclipse-based IDE is starting to look pretty sharp. See http://herzberg.ca.sandia.gov/jess .



By the way if I gave a command and it is thinking, how do I know if it is working on it or is simply not returing and not doing anything?


I guess you could run "top" in another terminal window, and then you'd see if your command was using CPU time or just sitting there waiting for input.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
By the way if I gave a command and it is thinking, how do I know if it is working on it or is simply not returing and not doing anything?


While top would work, you should see results within a few seconds to a minute, depending on how busy your system is or if you have to search through a lot of files. I would narrow the search to make it run faster as well as eliminate alot of unneccesary searches ..


This can be run from anywhere on the system.


This will "find" only jsp(s) from the current directory.


This will start the search from root "/". This will take the longest to complete.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Be careful, there, cj. The wildcards inside double quotes will be expanded immediately, before the commands in backticks are executed. Therefore you'll be asking find to find files in various places whose names match the JSP files in the current directory only. Using single quotes as I did prevents the wildcards from being expanded until the find program gets them; they'll then be used as a glob pattern in every directory find searches, which is what you want.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
The wildcards inside double quotes will be expanded immediately, before the commands in backticks are executed. Therefore you'll be asking find to find files in various places whose names match the JSP files in the current directory only.


I agree with most of what you said but I have to respectfully disagree with the above statement. When you issue a comand on the command line, special characters are seen by the shell, then by the program, therefore "unqouted metacharacters" are interpreted by the shell for filename expansion. In order to bypass the shell and pass the the special characters to find or grep, use quotes. Double quotes will suffice in most cases, but single qoutes are the safest bet.

In the examples I gave double qoutes are sufficient and also the use of double qoutes at least in my examples will not confine the search only in the current directory.

find /
find /Target
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I agree too - from experience I can say double quotes work here too.

I just liked to introduce $() instead of backticks, which usage is discouraged for bash.
So the command would look like:


which is sometimes more easy to type than backticks, and allways more easy to nest.

But for a lot of results from find, the commandline for grep will get too long, so I would encourage:


which will process file by file.
[ January 17, 2005: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
or, alternatively:



which hopefully should be very portable to other shells and find(1)'s. also, xargs(1) handles breaking up overlong command lines into multiple invocations as needed.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by cj jack:

I agree with most of what you said but I have to respectfully disagree with the above statement.


You are right. My apologies.
 
jQuery in Action, 2nd edition
 
subject: grep question
 
Similar Threads
IO
Multiple doc upload in single browse click
I/O Question.
diff in two files in UNIX,, help me
What is the concept of endorsed directory??