Package io.jawk

Class Awk.AwkRunBuilder

java.lang.Object
io.jawk.Awk.AwkRunBuilder
Enclosing class:
Awk

public final class Awk.AwkRunBuilder extends Object
Fluent builder for configuring and executing an AWK script or program.

Obtain an instance through Awk.script(String) or Awk.script(AwkProgram), configure input, arguments, and variables, then call one of the terminal methods to execute.


 // Execute and capture printed output as a String
 String result = awk.script("{ print toupper($0) }").input("hello").execute();

 // Execute to a specific stream
 awk.script(program).input(stream).execute(outputStream);

 // Execute with a custom sink
 awk.script("{ print $1 }").input(source).execute(mySink);

 // Execute to an appendable
 awk.script("{ print $1 }").input(source).execute(appendable);
 
  • Method Details

    • script

      public Awk.AwkRunBuilder script(String scriptText)
      Appends an additional AWK script to compile and execute. Multiple scripts are concatenated, like multiple -f options in the CLI.
      Parameters:
      scriptText - AWK program source
      Returns:
      this builder
      Throws:
      IllegalStateException - if a precompiled program was already set
    • input

      public Awk.AwkRunBuilder input(String input)
      Sets the text input to process.
      Parameters:
      input - text input (encoded as UTF-8 internally)
      Returns:
      this builder
    • input

      public Awk.AwkRunBuilder input(InputStream input)
      Sets the byte-stream input to process.
      Parameters:
      input - byte stream, or null for no input
      Returns:
      this builder
    • input

      public Awk.AwkRunBuilder input(InputSource source)
      Sets a structured InputSource to process.
      Parameters:
      source - structured record source
      Returns:
      this builder
    • arguments

      public Awk.AwkRunBuilder arguments(List<String> args)
      Sets runtime arguments visible through ARGC/ARGV.
      Parameters:
      args - runtime arguments
      Returns:
      this builder
    • arguments

      public Awk.AwkRunBuilder arguments(String... args)
      Sets runtime arguments visible through ARGC/ARGV.
      Parameters:
      args - runtime arguments
      Returns:
      this builder
    • argument

      public Awk.AwkRunBuilder argument(String arg)
      Adds a single runtime argument visible through ARGC/ARGV.
      Parameters:
      arg - runtime argument
      Returns:
      this builder
    • errorStream

      public Awk.AwkRunBuilder errorStream(PrintStream stream)
      Sets the stream used for the stderr output of spawned processes (e.g. system("...")).

      When not set, process stderr is merged into the main output sink. The CLI sets this explicitly to System.err so that command errors appear on the console rather than being mixed with normal output.

      Parameters:
      stream - stream to receive process stderr
      Returns:
      this builder
    • variables

      public Awk.AwkRunBuilder variables(Map<String,Object> overrides)
      Sets per-call variable overrides applied on top of the settings-level variables.
      Parameters:
      overrides - variable assignments (may be null)
      Returns:
      this builder
    • variable

      public Awk.AwkRunBuilder variable(String name, Object value)
      Sets a single per-call variable override.
      Parameters:
      name - variable name
      value - variable value
      Returns:
      this builder
    • execute

      public String execute() throws IOException, ExitException
      Executes the script and returns the printed output as a String.
      Returns:
      printed output
      Throws:
      IOException - if compilation or execution fails
      ExitException - if the script terminates with a non-zero exit code
    • execute

      public void execute(AwkSink sink) throws IOException, ExitException
      Executes the script, sending output to the specified AwkSink.
      Parameters:
      sink - output sink
      Throws:
      IOException - if compilation or execution fails
      ExitException - if the script terminates with a non-zero exit code
    • execute

      public void execute(PrintStream out) throws IOException, ExitException
      Executes the script, sending output to the specified PrintStream.
      Parameters:
      out - print stream (e.g. System.out)
      Throws:
      IOException - if compilation or execution fails
      ExitException - if the script terminates with a non-zero exit code
    • execute

      public void execute(OutputStream out) throws IOException, ExitException
      Executes the script, sending output to the specified OutputStream.
      Parameters:
      out - output stream
      Throws:
      IOException - if compilation or execution fails
      ExitException - if the script terminates with a non-zero exit code
    • execute

      public void execute(Appendable appendable) throws IOException, ExitException
      Executes the script, sending output to the specified Appendable (such as StringBuilder or StringWriter).
      Parameters:
      appendable - output destination
      Throws:
      IOException - if compilation or execution fails
      ExitException - if the script terminates with a non-zero exit code