This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using Unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Unix "grep" command in java..." Watch "Using Unix "grep" command in java..." New topic
Author

Using Unix "grep" command in java...

vasu devan
Ranch Hand

Joined: Apr 18, 2002
Posts: 37
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

Joined: Oct 12, 2001
Posts: 1179

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 ]

Regards, Rene Larsen
Dropbox Invite
vasu devan
Ranch Hand

Joined: Apr 18, 2002
Posts: 37
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

Joined: Mar 19, 2002
Posts: 405
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

Joined: Jul 27, 2001
Posts: 1365
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

Joined: Mar 19, 2002
Posts: 405
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

Joined: Jul 04, 2012
Posts: 4
use grep4j, it will do it for you
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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

Joined: Mar 17, 2011
Posts: 7554
    
  18

Stuart A. Burkett wrote:After 10 years, Vasu's probably not that bothered anymore.

Doh-h-h!!

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using Unix "grep" command in java...
 
Similar Threads
any java method which can work like grep command in unix
to check and start a process
please give me some hint of how to make an efficient algorithm to parse the log file
how to fetch rows based on line numbers or based on the beginning of a word
How to Get processes list from Remote Machine