Forums Register Login

any java method which can work like grep command in unix

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Check out java.util.regex.Pattern.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for your suggestion!
+Pie Number of slices to send: Send
You're welcome.
+Pie Number of slices to send: Send
 

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
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3863 times.
Similar Threads
Running UNIX command from Java
Piped output stream hangs on flush
Executing Unix command in Java
Using Unix "grep" command in java...
exec() method is limited in function
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:18:58.