2010-04-16 3 views
1

셸의 동일한 파일에서 grep을 호출 한 결과와 비교하여 Java 내부에서 grep을 호출하면 잘못된 결과가 나타납니다.Java에서 grep을 호출하면 잘못된 결과가 발생합니다. 셸에서 grep을 호출하면 올바른 결과가 나타납니다.

/bin/grep -vP --regexp='^[0-9]+\t.*' /usr/local/apache-tomcat-6.0.18/work/Catalina/localhost/saccitic/237482319867147879_1271411421 

자바 코드 :

String filepath = "/path/to/file"; 
String options = "P"; 
String grepparams = "^[0-9]+\\t.*"; 
String greppath = "/bin/"; 

String[] localeArray = new String[] { 
    "LANG=", 
    "LC_COLLATE=C", 
    "LC_CTYPE=UTF-8", 
    "LC_MESSAGES=C", 
    "LC_MONETARY=C", 
    "LC_NUMERIC=C", 
    "LC_TIME=C", 
    "LC_ALL=" 
}; 

options = "v"+options; //Assign optional params 

if (options.contains("P")) { 
    grepparams = "\'"+grepparams+"\'"; //Quote the regex expression if -P flag is used 
} else { 
    options = "E"+options; //equivalent to calling egrep 
} 

proc = sysRuntime.exec(greppath+"/grep -"+options+" --regexp="+grepparams+" "+filepath, localeArray); 
System.out.println(greppath+"/grep -"+options+" --regexp="+grepparams+" "+filepath); 
inStream = proc.getInputStream(); 

이 명령이 일치 폐기하도록되어

내 grep 명령 (. 자바와 bash는 모두이라고 나는 그에 따라 자바 슬래시 탈출) 다음과 같은 문자열 :

85295371616 Hi Mr Lee, please be informed that... 

입력 파일 :

85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
8~!95371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
852&^*&1616 Hi Mr Lee, please be informed that... 
8529537Ax16 Hi Mr Lee, please be informed that... 
85====ppq16 Hi Mr Lee, please be informed that... 
85291234783 a3283784428349247233834728482984723333 
85219299222 

명령 617,451,515,내가 내 bash는 (아래 결과)에서 호출 할 때 작동 : 나는 자바 안에 다시 그렙를 호출 할 때, 그러나

85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
8~!95371616 Hi Mr Lee, please be informed that... 
852&^*&1616 Hi Mr Lee, please be informed that... 
8529537Ax16 Hi Mr Lee, please be informed that... 
85====ppq16 Hi Mr Lee, please be informed that... 
85219299222 

, I (아래 결과) 전체 파일을 얻을 :

85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85aaa234567 Hi Ms Chan, please be informed that... 
85292vx5678 Hi Mrs Ng, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
8~!95371616 Hi Mr Lee, please be informed that... 
85295371616 Hi Mr Lee, please be informed that... 
852&^*&1616 Hi Mr Lee, please be informed that... 
8529537Ax16 Hi Mr Lee, please be informed that... 
85====ppq16 Hi Mr Lee, please be informed that... 
85291234783 a3283784428349247233834728482984723333 
85219299222 

Java에서 호출 된 grep이 잘못된 결과를 반환하도록하는 데 어떤 문제가 있습니까? runtime.exec에서 환경 문자열 배열을 통해 로컬 정보를 전달하려고 시도했지만 아무 것도 변경되지 않는 것 같습니다. 로켈 정보를 잘못 전달하고 있습니까? 아니면 완전히 다른 문제입니까?

+0

Java 코드를 표시하십시오. –

+0

왜 Java 내에서 grep을 호출합니까? Pattern 클래스를 사용하고 셸에 외부 호출을하지 않아도되는 이유는 무엇입니까? – Adamski

+0

프로덕션 환경에서 500k + 회선이있는 많은 파일을 처리 할 것이므로 외부 도구를 사용해야합니다. – futureelite7

답변

2

작은 따옴표는 셸에서 사용하며 grep에서는 표시되지 않습니다. 자바에 추가해서는 안됩니다.

또한 매개 변수를 연결하는 대신 exec (String [], String []) 메서드를 사용해야 매개 변수의 분리를 제어 할 수 있습니다.

그리고 다른 프로세스에서 grep을 시작하는 대신 순수한 java에서이 작업을 수행해야한다는 다른 의견에 동의합니다.

+0

따옴표를 제거하면 트릭을 만들었습니다. 감사! – futureelite7