-1

I am trying to remove the text between two line numbers. I am using the dmesg in Ubuntu, which is a really long command, so I would like to trim the string between line 0 3 but leave 4 through 5, etc. Here is my code:

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

            }

Any suggestions? Thanks!

0101011
  • 329
  • 2
  • 7
  • 14
  • I don't see the code were you try to remove the lines. All I see is code that reads the lines. Maybe create a line counter that you increment every time you read a line. Then you only outline lines greater than 3 – camickr Apr 27 '13 at 00:55

1 Answers1

1

Try this:

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);
    }
Voicu
  • 16,921
  • 10
  • 60
  • 69
  • Sorry, that was a bad example I am trying to get the last 6 lines of the output. Sorry, could you please help me find the solution? Thanks! – 0101011 Apr 27 '13 at 00:59
  • You've been given some sample code, why don't you try to solve the problem yourself. Don't expect others to write the code for you. – camickr Apr 27 '13 at 01:00
  • The only reason I asked was because I didn't know how to, – 0101011 Apr 27 '13 at 01:01
  • http://stackoverflow.com/questions/4121678/java-read-last-n-lines-of-a-huge-file might have what you need. – Voicu Apr 27 '13 at 01:03
  • So you've been given an example to start with. Now play around until it works. You are the only one that knows the format of your output and your exact requirement. You where given an answer to your original question. If the requirement changes then change the answer to match your new requirement. – camickr Apr 27 '13 at 01:03
  • @01100011_0101011_0101011 - What do you mean you don't know how? This is a trivial Java programming task. StackExchange is a programming Q&A site ... for people who know how to program, or are trying to learn to program. It is not a place for getting someone else to do your programming for you!!! – Stephen C Apr 27 '13 at 01:09