【问题标题】:awk commands in JavaJava中的awk命令
【发布时间】:2017-01-17 19:05:59
【问题描述】:

我想在 Java 中使用 awk 命令行。为此,我有这段代码(我在教程中看到了这段代码):

Map map = new HashMap();
map.put("file", new File(fileDirectory)); 

CommandLine cmdLine = new CommandLine("awk"); 
cmdLine.addArgument("{print $1}", false); 
cmdLine.addArgument("${file}"); 
cmdLine.setSubstitutionMap(map);
System.out.println(cmdLine.toString()); 

DefaultExecuteResultHandler resultHandler = new   DefaultExecuteResultHandler();  
ExecuteWatchdog watchdog = new ExecuteWatchdog(10000); 
DefaultExecutor executor = new DefaultExecutor(); 
executor.setWatchdog(watchdog); 
executor.execute(cmdLine, resultHandler); 

resultHandler.waitFor(); 

在本例中,我的代码打印文件的第一列。

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10

我想将输出打印到一个文件中,但我想不通。

【问题讨论】:

  • 这似乎是一个 XY 问题。 Java 拥有读取文件和从每一行提取第一个标记所需的所有工具

标签: java file awk


【解决方案1】:

一般来说,要在 Java 中以编程方式运行 awk 命令,您可以使用Jawk。我被要求自己建造它,但没什么大不了的。我已经使用 1.03 版进行了测试,可以为我的用例以编程方式调用 awk 命令,如下所示。

            String[] awkArgs= { "-f", awkProgramPath };
            // Create a stream to hold the output
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            // Tell Java to use your byte arrya output stream temporarily
            System.setOut(ps);

            AwkParameters parameters = new AwkParameters(Main.class, null);
            AwkSettings settings = parameters.parseCommandLineArguments(awkArgs);

            //Instead of passing file as argument setting the file content as input. 
            //This is done to adapt the line delimiters in file content to the specific OS. Otherwise Jawk fails in splitting the lines.
            //If it is not required, this step cab be skipped and the input file path can be passed as additional argument
            String inputFileContent = new String(Files.readAllBytes(inputFile.toPath())); 
            //Adapting line delimiter.
            inputFileContent = inputFileContent.replaceAll("[\r]?\n", System.getProperty("line.separator"));
            settings.setInput(new ByteArrayInputStream(inputFileContent.getBytes()));

            //Invoking the awk command
            Awk awk = new Awk();
            awk.invoke(settings);
            //Set the old print stream back. May be you can do it in try..finally block to be failsafe.
            System.setOut(sysout);
            //Get the output from byte array output stream
            String output = baos.toString();

您可能需要将 slf4j.jar 添加到 jak.jar 内部所需的类路径中。

【讨论】:

    【解决方案2】:

    我从来没有用过DefaultExecutor,但你可以像下面那样做

    Process process = Runtime.getRuntime().exec(cmdLine.toString());
    InputStream stdout = process.getInputStream();
    
    BufferedReader stdout_br = new BufferedReader(new InputStreamReader(stdout));
    String tmp;
    StringBuilder cmp_stb = new StringBuilder();
    while ((tmp = stdout_br.readLine()) != null) {
       cmp_stb.append(tmp + "\n");
    }
    stdout_br.close();
    

    将字符串保存到文件中。
    或者,由于您只是提供一个字符串命令,您还可以在命令中附加一个像“>filename”这样的文件重定向并执行。

    【讨论】:

    • 我试图不使用 getRuntime().exec() 因为我读到了一些关于该命令的 cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多