• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Unix "grep" command in java...

 
vasu devan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I would like to know how to use the Unix grep command in java. So that I grep for the particular text in the configuration file and use it in my program.
The general grep command would be

cat FileName | grep Status

By using the above command I can pick out the status from the specified file. I need to use this in my java program so that I get the status in run time. Can anyone help me out in solving this.

Thanks in Advance,
Vasu.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can use:

If you want to use it in Windows you can download some UnixUtil
/Ren´┐Ż
[ June 12, 2002: Message edited by: Rene Larsen ]
 
vasu devan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rene Larsen,
Thanks for your reply. I could read the total content in the file. But I want to read only the status which is in the middle of the file.
As we do selected reading using the command,

`cat $Filename | grep status | cut -d"=" -f2`

is not working in java.

At present I am putting the above command in a shell script and executing that in java.
Is there a way where I can directly use this command java.
Thanks,
Vasu.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you will be able to get the variable $FILENAME to work, because in unix $filename is defined as either a global variable i.e. environment variable or a local variable. And I don't think you want to define $filename as a global variable. Because end your java code when you invoke the unix environment your creating a new child process, which you will only have access to global variables or variables created by the parent. So I suggest you pass the physical name of the file you want to grep on. Also your command contains special java characters, which you will have to prepend with the "\"(escape character) such as
... -d\"=\" ....
My 2 cents.
Craig
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look like you're using regular expressions in you're grep search string, so you might consider just reading off lines with BufferedReader.readLine() and ignoring ones that don't include the substring 'status'. I don't know anything about the 'cut' command though.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What the command
cat $FILENAME | grep status | cut -d"=" -f2
is trying to do is find all occurences of "status" inside $FILENAME, then pipe the output of field 2, which can be found by specifing the delimiter, which in this case is a "=" sign.

An example of this would be something like this:
File:
the status of job2 = inactive<nl>
the status of job1 = active<nl>
The unix command would return inactive and active.
Hope this helps.
craig.
 
marco castigliego
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use grep4j, it will do it for you
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasu devan wrote:At present I am putting the above command in a shell script and executing that in java.
Is there a way where I can directly use this command java.

I think you need to decide what you want. A shell script can certainly do the job, but it's a platform-specific solution.

My suggestion is: if you want to write it in Java, write it all in Java; the language certainly has the ability to do what you want, but you will have to read the entire file.

Rene's post has already shown you the basic method for reading lines. Have a look at String.matches() and String.split(), along with the Pattern (java.util.regex.Pattern) and Matcher (java.util.regex.Matcher) classes.
Between them, they will do anything you can do with grep (and probably quite a bit more).

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
vasu devan wrote:At present I am putting the above command in a shell script and executing that in java.
Is there a way where I can directly use this command java.

I think you need to decide what you want.

After 10 years, Vasu's probably not that bothered anymore.
 
Winston Gutkowski
Bartender
Pie
Posts: 10268
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:After 10 years, Vasu's probably not that bothered anymore.

Doh-h-h!!

Winston
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the thread is 10 yrs old, but why didn't anybody point out that you don't have to cat and pipe the file to grep? This is all you need:

grep status $Filename
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic