Package io.jawk
Class Awk.AwkRunBuilder
java.lang.Object
io.jawk.Awk.AwkRunBuilder
- Enclosing class:
- Awk
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 Summary
Modifier and TypeMethodDescriptionAdds a single runtime argument visible throughARGC/ARGV.Sets runtime arguments visible throughARGC/ARGV.Sets runtime arguments visible throughARGC/ARGV.errorStream(PrintStream stream) Sets the stream used for the stderr output of spawned processes (e.g.system("...")).execute()Executes the script and returns the printed output as aString.voidExecutes the script, sending output to the specifiedAwkSink.voidexecute(OutputStream out) Executes the script, sending output to the specifiedOutputStream.voidexecute(PrintStream out) Executes the script, sending output to the specifiedPrintStream.voidexecute(Appendable appendable) Executes the script, sending output to the specifiedAppendable(such asStringBuilderorStringWriter).input(InputSource source) Sets a structuredInputSourceto process.input(InputStream input) Sets the byte-stream input to process.Sets the text input to process.Appends an additional AWK script to compile and execute.Sets a single per-call variable override.Sets per-call variable overrides applied on top of the settings-level variables.
-
Method Details
-
script
Appends an additional AWK script to compile and execute. Multiple scripts are concatenated, like multiple-foptions in the CLI.- Parameters:
scriptText- AWK program source- Returns:
- this builder
- Throws:
IllegalStateException- if a precompiled program was already set
-
input
Sets the text input to process.- Parameters:
input- text input (encoded as UTF-8 internally)- Returns:
- this builder
-
input
Sets the byte-stream input to process.- Parameters:
input- byte stream, ornullfor no input- Returns:
- this builder
-
input
Sets a structuredInputSourceto process.- Parameters:
source- structured record source- Returns:
- this builder
-
arguments
Sets runtime arguments visible throughARGC/ARGV.- Parameters:
args- runtime arguments- Returns:
- this builder
-
arguments
Sets runtime arguments visible throughARGC/ARGV.- Parameters:
args- runtime arguments- Returns:
- this builder
-
argument
Adds a single runtime argument visible throughARGC/ARGV.- Parameters:
arg- runtime argument- Returns:
- this builder
-
errorStream
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.errso 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
Sets per-call variable overrides applied on top of the settings-level variables.- Parameters:
overrides- variable assignments (may benull)- Returns:
- this builder
-
variable
Sets a single per-call variable override.- Parameters:
name- variable namevalue- variable value- Returns:
- this builder
-
execute
Executes the script and returns the printed output as aString.- Returns:
- printed output
- Throws:
IOException- if compilation or execution failsExitException- if the script terminates with a non-zero exit code
-
execute
Executes the script, sending output to the specifiedAwkSink.- Parameters:
sink- output sink- Throws:
IOException- if compilation or execution failsExitException- if the script terminates with a non-zero exit code
-
execute
Executes the script, sending output to the specifiedPrintStream.- Parameters:
out- print stream (e.g.System.out)- Throws:
IOException- if compilation or execution failsExitException- if the script terminates with a non-zero exit code
-
execute
Executes the script, sending output to the specifiedOutputStream.- Parameters:
out- output stream- Throws:
IOException- if compilation or execution failsExitException- if the script terminates with a non-zero exit code
-
execute
Executes the script, sending output to the specifiedAppendable(such asStringBuilderorStringWriter).- Parameters:
appendable- output destination- Throws:
IOException- if compilation or execution failsExitException- if the script terminates with a non-zero exit code
-