我正在嘗試使用 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(); }}請提供一些有關如何在java中使用awk的幫助?如果有其他方法可以做到嗎?
3 回答

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
只是一個建議,因為由于聲譽點不足,我仍然無法發表評論。你為什么不創建一個本地 shell 腳本并在awk
那里執行你的命令并使用 java 來觸發所說的 shell 腳本?我覺得這樣更方便。在這個問題中,您想awk
在 java 中使用,但實際上它是一個可以在 shell 腳本中使用的“命令行實用程序”。如果您真的想awk
在 java 中使用,那么可以嘗試搜索第三方庫。
添加回答
舉報
0/150
提交
取消