java - 在 Java 代码中使用 Grep

标签 java string text grep

我有两个相同的数组,Noun 和 Noun1,并且想要查找并提取包含单词组合的行(parliamenr 评论)。我想在linux中使用grep命令,我编写了以下代码:

        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);
        }

    }
}

问题是 id1 和 id2 不引用数组元素,并且命令搜索在文本文件中查找 id1 和 id2。 String[] cmd = { "/bin/sh", "-c", "grep\"id1 id2\"/local//wiki-pmi/*.txt"}; 任何人都可以帮我更改代码,使 id1 和 id1 引用数组(名词和 Nooun1)元素吗?

最佳答案

我的建议:

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);

但不知道它是否真的有效,因为我没有了解你的背景。

关于java - 在 Java 代码中使用 Grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188142/

相关文章:

c - 将字符串数组分配和检索给 void 指针

Python字符串精确结束,没有附加字符

C# for循环逐行写入

java - 数据存储区/动态文本文件中的 Google App Engine (Java) 文本

r - 使用 R 将列名插入其值

java - 如何在 gradle 中创建 zip 时创建空文件夹

java - 如何链接两个 jcomboboxes 而不重复值

javascript - 带有 JavaScript 的 ffmpeg 缩略图

java - Java中的RSA加密算法 : no BigIntegers

sql - 将字符串拆分成行Oracle SQL