sed 명령이 Java 코드의 Centos에서 작동하지 않습니다.

sed 명령이 Java 코드의 Centos에서 작동하지 않습니다.

Centos에서 BOM을 삭제하기 위해 파일 sed에 명령을 실행하는 Java 코드를 실행하고 있습니다 . .txt다음 오류가 발생합니다.

sed: -e expression #1, char 1: unknown command: `''

자바 코드:

        String[] args = {"sed","-i","'1s/^\\xEF\\xBB\\xBF//'","/tmp/output.txt"};
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process p = processBuilder.start();
        int exitVal = p.waitFor();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));


        String line = null;
        // read the output from the command
        while ((line = stdInput.readLine()) != null) {
            output.append(line + "\n");
        }
        // read any errors from the attempted command
        logger.info("Here is the standard error of the command (if any):\n");
        while ((line = stdError.readLine()) != null) {
            logger.info("Error: " + line);
        }

답변1

오류 메시지는 명령(작은따옴표, 표현식의 첫 번째 문자)이 무엇인지 sed알 수 없음 을 나타냅니다.'

표현식 주위의 작은따옴표를 제거합니다 sed. 문자열이 셸에서 해석되지 않도록 보호하기 위해 셸에서 이것이 필요할 수 있지만 sed다른 언어에서 호출할 때는 필요하지 않습니다.

또한 Java나 다른 언어에서 외부 도구를 호출하는 것은 약간 어색해 보입니다. 아마도 일부 라이브러리를 사용하여 언어 내에서 직접 이러한 유형의 수정을 수행할 수 있다고 생각합니다.

관련 정보