2013-04-27 3 views
-1

두 줄 번호 사이의 텍스트를 제거하려고합니다. 나는 정말로 긴 명령입니다 우분투에서 dmesg를 사용하고, 그래서 라인 0 ~ 3 사이의 문자열을 트리밍 좋아하지만 4-5을 떠날 것 등이 여기에 내 코드입니다 :Java - 줄 사이의 텍스트 제거

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

      } 

어떤 제안이? 감사!

+0

나는 코드가 당신이 줄을 제거하려고했다 표시되지 않습니다. 내가 보는 모든 것은 선을 읽는 코드입니다. 라인을 읽을 때마다 증가하는 라인 카운터를 만들 수 있습니다. 그 다음에는 3보다 큰 행만 윤곽을 나타냅니다. – camickr

답변

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

죄송합니다. 출력의 마지막 6 행을 얻으려고하는 나쁜 예입니다. 죄송합니다. 해결책을 찾도록 도와 주시겠습니까? 감사! – 0101011

+0

몇 가지 샘플 코드를 받았으니 직접 문제를 해결해 보시기 바랍니다. 다른 사람들이 당신을 위해 코드를 작성할 것으로 기대하지 마십시오. – camickr

+0

내가 물었던 유일한 이유는 내가 어떻게 해야할지 모르기 때문입니다. – 0101011

관련 문제