我写了一条命令
for i in `cat ./files/logs1.txt | grep -o '[[:digit:]]*' | cut -c 1-3 | sort| uniq -c | sort -nr | sed 1d | head -n 10 | awk '{print $2}' `; do grep ^$i ./files/logs1.txt > $i.txt; done
从终端运行得很好。但是我需要使用Java运行它。我已经尝试过使用Runtime.getRuntime()。exec()和ProcessBuilder,但是我不确定如何将该命令分解为字符串数组来帮助我运行它。
String myCommand = "for i in `cat ./files/logs1.txt | grep -o '[[:digit:]]*' | cut -c 1-3 | sort| uniq -c | sort -nr | sed 1d | head -n 10 | awk '{print $2}' `; do grep ^$i ./files/logs1.txt > $i.txt; done";
Process p = r.exec(new String[] { "bash", "-c", myCommand });
p.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();