• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

any java method which can work like grep command in unix

 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.regex.Pattern.
 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion!
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic