Implementing unix "grep" command in java

G

Guest

Hi..All,

I have a request from my user to list of the files from the folders
which has got the string he is searching for. How do I implement the
unix command grep in JAVA. or are there any methods/interfaces in java
that can scan file(s) in a given folder & do the same functionality?

Regards,

P
 
E

Eric Sosman

nospam said:
Hi..All,

I have a request from my user to list of the files from the folders
which has got the string he is searching for. How do I implement the
unix command grep in JAVA. or are there any methods/interfaces in java
that can scan file(s) in a given folder & do the same functionality?

You have the machinery of java.io.File to locate the
files in a directory, and other machinery in java.io to
open them and read their contents. The java.util.regex
package provides classes and methods that should be helpful
in detecting the presence of the desired strings -- or if
they're really just strings and not regular expressions,
you might simply use the indexOf(String) method of String.
 
R

Roland

Hi..All,

I have a request from my user to list of the files from the folders
which has got the string he is searching for. How do I implement the
unix command grep in JAVA. or are there any methods/interfaces in java
that can scan file(s) in a given folder & do the same functionality?

Regards,

P
Grep.java at
<http://java.sun.com/j2se/1.5.0/docs/guide/nio/example/index.html>
Don't know how it compares to Unix' grep.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
H

HK

nospam said:
I have a request from my user to list of the files from the folders
which has got the string he is searching for. How do I implement the
unix command grep in JAVA. or are there any methods/interfaces in java
that can scan file(s) in a given folder & do the same functionality?

How about:

http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/programs/Grep.html

It has a main() to run it from the command line,
but all the functionality is encapsulated in
a class too. The concept of `line' is, however,
replaced by the concept of `region of interest',
which makes it a nice tool for XML grepping.

Harald.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top