• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

any java method which can work like grep command in unix

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out java.util.regex.Pattern.
 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your suggestion!
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic