• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

grep question

 
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

grep -R 'isBabyBornYet' *.jsp



That works for me. Have you CD'd to a directory that has jsp files, or folders containing jsps?
 
vasu maj
Ranch Hand
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,
And the command you've posted does just that for me.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*.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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 398
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic