Jawk 7.1.01
-
Home
- Jawk Java API
Jawk in Java
For most Java applications, start with Awk[1]. It gives you:
- short convenience methods for string-in, string-out use cases
- compiled
AwkProgramandAwkExpressionartifacts for reuse - direct access to
AVM[2] when you want one reusable runtime
Start with Awk
Create an Awk instance directly for normal use:
Awk awk = new Awk();
Construct it with AwkSettings when you need engine defaults such as field separators, locale, or record separators:
AwkSettings settings = new AwkSettings();
settings.setFieldSeparator(",");
Awk awk = new Awk(settings);
AwkSettings Reference
| Setter | Default | Description |
|---|---|---|
setFieldSeparator(String) |
null (default AWK FS) |
The initial value of FS, the field separator |
setLocale(Locale) |
Locale.US |
Locale for numeric output formatting |
setDefaultRS(String) |
"\n" |
Default value for RS, the record separator |
setUseSortedArrayKeys(boolean) |
false |
Whether to keep associative array keys in sorted order |
setPosix(boolean) |
false |
Enforce POSIX compile-time behavior, rejecting gawk syntax such as a[i][j], split(..., a[i]), all @ forms, and the BEGINFILE / ENDFILE special patterns |
putVariable(String, Object) |
Empty map | Pre-set variables available before BEGIN |
The output destination is not a setting: it is chosen per call, when you execute the script. See Custom Output[3]. For more on passing variables to scripts, see Variables and Arguments[4].
By default, Jawk accepts classic AWK syntax and the gawk extensions it implements[5]. Enable POSIX mode when you need strict classic parsing:
AwkSettings settings = new AwkSettings();
settings.setPosix(true);
Awk awk = new Awk(settings);
Construct it with extension instances when you want those functions available to the script:
Awk awk = new Awk(new GawkExtension(), new MyExtension());
A plain new Awk() enables the built-in GNU Awk compatibility extension[6] (asort(), typeof(), and friends). Passing explicit extension instances replaces that default set, so include a new GawkExtension() in the list when the script still needs the gawk builtins.
The dedicated Writing Extensions[7] guide covers how to write your own extensions to expose new functions, written in Java, to your AWK scripts.
The Shortest Path: script().execute()
script().execute() is the smallest API surface for full AWK programs when you want the printed output back as a Java String:
Awk awk = new Awk();
String result = awk.script("{ print toupper($0) }").input("hello world").execute();
// result = "HELLO WORLD\n"
Use this when:
- you already have the script and input in memory
- you want the rendered AWK output as a
String - you do not need explicit
ARGV, per-execution variables, or runtime reuse
Compiled Programs
When the same script will be reused, compile it once and run the compiled program:
Awk awk = new Awk();
AwkProgram program = awk.compile("{ print prefix $1 }");
awk.script(program)
.input("alpha beta\n")
.execute();
Output Destination
Where the script's output goes is decided by the execute(...) overload you call:
execute()returns the printed output as aStringexecute(PrintStream)sends output to aPrintStreamsuch asSystem.outexecute(OutputStream)sends output to anyOutputStreamexecute(Appendable)captures text into aStringBuilderorAppendableexecute(AwkSink)hands rawprint/printfcalls to your ownAwkSink[8], so the host can collect structured values instead of rendered text
See the Custom Output[3] guide for the full AwkSink contract, the built-in implementations, and locale handling.
Reusable Runtime: AVM
When you want to keep the same runtime alive across several calls, create an AVM:
Awk awk = new Awk();
AwkProgram program = awk.compile("BEGIN { print \"value\" }");
try (AVM avm = awk.createAvm()) {
avm.setAwkSink(mySink);
avm.execute(program, myInputSource, Collections.<String>emptyList(), null);
avm.execute(program, myOtherInputSource);
}
AVM is sequential-only and intentionally stateful. Use it when performance matters and you want one reusable runtime for repeated program runs or repeated expression evaluation.
Which API Should I Use?
script(text).input(text).execute()for the shortest string-in, string-out path.compile(...)plusscript(compiled).execute(out)when a whole AWK program is reused.compileExpression(...)pluseval(...)when one expression is reused.createAvm()when you want one reusable runtime across several calls.
Complete Example
The example below reads CSV input, sums the second column per category in the first column, and captures the result:
import io.jawk.Awk;
import io.jawk.util.AwkSettings;
public class JawkDemo {
public static void main(String[] args) throws Exception {
// Configure the engine for CSV input
AwkSettings settings = new AwkSettings();
settings.setFieldSeparator(",");
Awk awk = new Awk(settings);
// AWK script: accumulate totals by category, print sorted results
String script = "{ totals[$1] += $2 } END { for (k in totals) print k, totals[k] }";
// Input data
String csv = "fruit,10\nvegetable,20\nfruit,15\nvegetable,5\n";
// Execute and capture the printed output
String result = awk.script(script).input(csv).execute();
System.out.println(result);
}
}
See Also
- Variables and Arguments[4]
- Structured Input[9]
- Custom Output[3]
- Compile, Eval, and Reuse[10]
- Advanced Runtime[11] — AVM reuse, sandboxing, JSR 223, and thread safety
- Using Extensions[12]
- [1] apidocs/io/jawk/Awk.html
- [2] apidocs/io/jawk/backend/AVM.html
- [3] java-output.html
- [4] java-variables.html
- [5] compatibility.html
- [6] extensions.html#gawk
- [7] extensions-writing.html
- [8] apidocs/io/jawk/jrt/AwkSink.html
- [9] java-input.html
- [10] java-compile.html
- [11] java-advanced.html
- [12] extensions.html
