Java Runtime Issue / Text File Searching

Discussion in 'Java' started by rajatag, Aug 14, 2006.

  1. rajatag

    rajatag Guest

    Hi folks,

    Here's a tricky one (I think):

    I need to search for words within a file. To do this, I thought of
    using grep and executing from Runtime.getRuntime() instead of writing
    something in Java.

    Now, when I run the following in linux:

    grep -c -x -F 'HOT' filetosearch.txt

    The program prints out 1 which is correct as the file specified above
    has just one occurrence of the word HOT on a single line. However, when
    I put the same thing within Java like this:

    Runtime rt = Runtime.getRuntime();
    Process pt = rt.exec("grep -c -x -F 'HOT' filetosearch.txt");

    Then the Java application always gets a 0 count.

    Any ideas why? Also, is there any better way to do searching of text
    files within java?
     
    rajatag, Aug 14, 2006
    #1
    1. Advertising

  2. On 14 Aug 2006 12:39:34 -0700, rajatag wrote:
    > Runtime rt = Runtime.getRuntime();
    > Process pt = rt.exec("grep -c -x -F 'HOT' filetosearch.txt");
    >
    > Then the Java application always gets a 0 count.
    >
    > Any ideas why? Also, is there any better way to do searching of text
    > files within java?


    When you run the command from a shell, it removes the single quotes
    from the search string and they aren't ever seen by grep.

    When you run the command from Java, there is no shell and grep is
    looking for the 5-characters 'HOT' (i.e. including the 2 single
    quotes), which presumably isn't in your file.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Aug 14, 2006
    #2
    1. Advertising

  3. rajatag

    rajatag Guest

    Thanks! I removed the quotes and it started to work fine!
     
    rajatag, Aug 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    277
  2. utab

    Searching text in a file

    utab, Feb 25, 2006, in forum: C++
    Replies:
    1
    Views:
    357
    Victor Bazarov
    Feb 25, 2006
  3. Umesh

    output all a* by searching a text file

    Umesh, May 15, 2007, in forum: C Programming
    Replies:
    19
    Views:
    567
    Keith Thompson
    May 20, 2007
  4. jagadesh

    Issue With File Searching

    jagadesh, Jan 10, 2009, in forum: Java
    Replies:
    2
    Views:
    276
    Roland Pibinger
    Jan 10, 2009
  5. stumblng.tumblr
    Replies:
    1
    Views:
    210
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page