gpt4 book ai didi

java - 在 Java 中调用 Grep 会给出错误的结果,而在 shell 中调用 grep 会给出正确的结果

转载 作者:行者123 更新时间:2023-11-30 05:13:07 32 4
gpt4 key购买 nike

我遇到一个问题,与在 shell 中对同一文件调用 grep 的结果相比,从 java 内部调用 grep 给出的结果不正确。

我的 grep 命令(在 Java 和 bash 中都调用。相应地,我在 Java 中转义了斜杠):

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

Java 代码:

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

该命令应该匹配并丢弃如下字符串:

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

当我从 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

但是,当我在 java 中再次调用 grep 时,我得到了整个文件(结果如下):

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中的环境字符串数组传递本地信息,但似乎没有任何改变。我是否错误地传递了区域设置信息,或者问题完全是其他问题?

最佳答案

单引号由 shell 使用,grep 不会看到。您不应该在 java 中添加它们。

此外,您应该使用 exec(String[], String[]) 方法而不是连接参数,这样您就可以控制参数的分离。

我同意其他评论,说你应该在纯 java 中执行此操作,而不是在单独的进程中启动 grep。

关于java - 在 Java 中调用 Grep 会给出错误的结果,而在 shell 中调用 grep 会给出正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2652113/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com