-1

我正在尝试删除两个行号之间的文本。我dmesg在 Ubuntu 中使用,这是一个非常长的命令,所以我想修剪第 0 3 行之间的字符串,但保留 4 到 5 等。这是我的代码:

            try {

                Process terminal = Runtime.getRuntime().exec("dmesg");
                BufferedReader in = new BufferedReader(new InputStreamReader(terminal.getInputStream()));
                String line;
                String output = "";

                while((line = in.readLine()) != null) {

                   output += line + "\n";

                }

                System.out.println(output);
                in.close();

            } catch(Exception exc) {

                System.err.println("An error occurred while executing a command in the terminal. Error:\n" + exc);

            }

有什么建议么?谢谢!

4

1 回答 1

1

尝试这个:

try {

        Process terminal = Runtime.getRuntime().exec("dmesg");
        BufferedReader in = new BufferedReader(new InputStreamReader(terminal.getInputStream()));
        String line;
        String output = "";
        int counter = 0;
        while((line = in.readLine()) != null) {
            if (counter > 3) output += line + "\n";
            counter++;
        }

        System.out.println(output);
        in.close();

    } catch(Exception exc) {

        System.err.println("An error occurred while executing a command in the terminal. Error:\n" + exc);
    }
于 2013-04-27T00:55:50.903 回答