我正在嘗試使用 java 中的 awk。我寫了下面的代碼,但沒有輸出。public class ShellScriptExecution { public static void main (String[] a) { ShellScriptExecution shellsc = new ShellScriptExecution(); String command = "awk '/Ayushi/ {print}' /home/ayushi/Desktop/testAwk.txt "; String op = shellsc.execute(command, a); System.out.println(op); } private String execute(String command, String[] a) { StringBuffer sb = new StringBuffer(); Process p; try { // // p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine())!= null) { sb.append(line + "\n"); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } return sb.toString(); }}請?zhí)峁┮恍┯嘘P(guān)如何在java中使用awk的幫助?如果有其他方法可以做到嗎?
3 回答

一只斗牛犬
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
只是一個(gè)建議,因?yàn)橛捎诼曌u(yù)點(diǎn)不足,我仍然無法發(fā)表評(píng)論。你為什么不創(chuàng)建一個(gè)本地 shell 腳本并在awk
那里執(zhí)行你的命令并使用 java 來觸發(fā)所說的 shell 腳本?我覺得這樣更方便。在這個(gè)問題中,您想awk
在 java 中使用,但實(shí)際上它是一個(gè)可以在 shell 腳本中使用的“命令行實(shí)用程序”。如果您真的想awk
在 java 中使用,那么可以嘗試搜索第三方庫。
添加回答
舉報(bào)
0/150
提交
取消