0

I have two identical arrays, Noun and Noun1, and want to find and extract the line that contains the combination of words (parliamenr reviews). I want to use grep command in linux and I wrote the following code:

        for(int i=0;i<noun.size();i++)
        {
            for(int j=0;j<noun1.size();j++)
            {

                String id1 = noun.get(i);
                 String id2 = noun1.get(j);

                System.out.println(id1 +"\t"+id2);

                Runtime rt = Runtime.getRuntime();
                String[] cmd = { "/bin/sh", "-c", "grep \"id1 id2\" /local//wiki-pmi/*.txt"};
                 Process proc = rt.exec(cmd);
        BufferedReader is = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                 String line;
                 while ((line = is.readLine()) != null) {
                     System.out.println(line);
                 }


            }
        }
        fis.close();
        }
        catch(Exception e){
        System.out.println(e);
        }

    }
}

The problem is id1 and id2 do not refer to Arrays elements and the command search to find id1 and id2 in the text files instead. String[] cmd = { "/bin/sh", "-c", "grep \"id1 id2\" /local//wiki-pmi/*.txt"}; Could Anyone help me to change the code in a way that id1 and id1 refer to the array (noun and Nooun1) elements?

5
  • You need to concatenate the string together: `"grep " + id1 + " " + id2 + ... Commented Aug 7, 2014 at 17:02
  • 2
    It is almost sure that this is not the right way to tackle the problem. Please explain what your two arrays contain and what you want to achieve. Commented Aug 7, 2014 at 17:02
  • @JamesKingsbery you probably mean: `"grep \""+id1+" " + id2 +"\"...`` Commented Aug 7, 2014 at 17:03
  • What does "parliamenr reviews" means ? Commented Aug 7, 2014 at 17:04
  • @alfasin Thanks for your response. "parliamenr reviews" are an example of the string query that I am going to find in the 6.6 GB text files. Do you know possibly what are options I have to handle such a giant files except HashMap and SQL which are nonfunctional? This way is very fast and efficient. If you know other way please let me know...Thanks Commented Aug 7, 2014 at 17:59

1 Answer 1

1

My Suggestion:

Runtime rt = Runtime.getRuntime();
String[] cmd = { "/bin/sh", "-c", "grep \"" + id1 + " " + id2 + "\" /local//wiki-pmi/*.txt"};
System.out.println("First command: " + cmd[0]);
Process proc = rt.exec(cmd);

Don't know if it really works, though, since I havn't got your context.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. It works now. However I don't understand why you wrote "System.out.println("First command: " + cmd[0])"? it just print "First Command :/bin/sh"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.