This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes any java method which can work like grep command in unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "any java method which can work like grep command in unix" Watch "any java method which can work like grep command in unix" New topic
Author

any java method which can work like grep command in unix

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Hi,

I need a help.

I want to search a string in a file which is in Unix server.I am able to connect that server from my java program and go to that directory where that file present using ChannelSftp class.

Like cd method of ChannelSftp works like cd command in unix.
ls method of ChannelSftp works like ls command in unix.

My requirement will fulfil if I get any method which works like grep command in unix.
Is there any method present so that I am able to grep.

Please give some idea.

Thanks in Advance!
Sidharth
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out java.util.regex.Pattern.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Thanks!

But my request is that "Is there any java method which can work like grep command in unix"

Simplyfying this:
My requirement is - I have to search a text string in a file which is present in unix system.

I am writing a java class in my windows os whose aim is to connect to the unix server and search the text in that file.

Now I am able to connect the unix server using a java class com.jcraft.jsch.ChannelSftp and go to that directory where that file exist and get the whole file to my java program.The methods are cd and get in ChannelSftp.

But this approach will be taking time as I have to search this file through java program.

But If I am able to use the grep command using java program , then I can pull only the required text which will be more effective.

Please give some idea.

I will be appreciated for your reply!

Thanks,
Sidharth
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Java itself doesn't have one single method like grep. You would really need to read the file line by line, and use Pattern on each separate line. It will be faster if you create the Pattern outside the loop.

You can call grep itself from Java using Runtime.exec() or ProcessBuilder. However, I doubt that this will be faster, as the JVM needs to spawn a new process for that.
siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Thanks for your suggestion!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

siddharth das wrote:But If I am able to use the grep command using java program , then I can pull only the required text which will be more effective.

Another thing you might want to consider is to split the operations up completely and run them in a script instead. Then you could pipe the output of your sftp command directly to a grep, and the result of that to your java program. You wouldn't have to worry about any of that com.jcraft.jsch.ChannelSftp stuff, and your program could concentrate on what it is (presumably) supposed to do: parse the output of the grep.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
GeeCON Prague 2014
 
subject: any java method which can work like grep command in unix