Can i use awk in java?

I tried the code below but it doesn't work. Nevertheless, if I try to cat the file, it works and prints the whole content of the file.

But the reason I tried using awk is that I don't need the whole content, I only need some parts of each line.

Runtime r = Runtime.getRuntime();
Process p = r.exec("awk -F\":\" '/in/ {print $3}' file.awk"); 
p.waitFor();
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("esto es una \"prueba\"");
String valor = "";
while ((valor = in.readLine())!= null){
    System.out.println(valor);
}

Upvotes: 1

Views: 3861

Answers (2)

Michael Vehrs
Michael Vehrs

Reputation: 3363

Why would you want to run awk for a problem that is so trivial? If the line contains the string "in", split it on ":" and return the third field:

BufferedReader reader = new BufferedReader(new FileReader(filename));
while(String line = reader.readLine()) {
    if (line.indexOf("in") >= 0) {
        String[] fields = line.split(":");
        System.out.println(fields[2]);
    }
}

Upvotes: 2

Yeasir Arafat Majumder
Yeasir Arafat Majumder

Reputation: 1264

jawk

is the java implementation of awk. Please follow this link for an overview: http://jawk.sourceforge.net/

Upvotes: 2

Related Questions