Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing unix "grep" command in java

340 views
Skip to first unread message

nospam

unread,
Apr 15, 2005, 11:33:32 AM4/15/05
to
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

Eric Sosman

unread,
Apr 15, 2005, 11:54:12 AM4/15/05
to

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.

--
Eric....@sun.com

Roland

unread,
Apr 15, 2005, 2:06:55 PM4/15/05
to
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_/ /__/
/ \ /_/ / \

HK

unread,
Apr 15, 2005, 3:11:02 PM4/15/05
to

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.

0 new messages