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.
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.
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
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.
Joined: Mar 19, 2002
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.
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).
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here