Package io.jawk.jrt

Class JavaStringFormatAwkSink


public class JavaStringFormatAwkSink extends OutputStreamAwkSink
Text AwkSink whose printf and sprintf use Java's standard String.format(Locale, String, Object...) instead of AWK's formatting rules.

This gives AWK scripts access to Java's formatting capabilities, such as %,d grouping, %(d negative parentheses, date/time conversions (%tY), and generally faster formatting. In exchange, the format string follows Formatter semantics, not POSIX AWK: conversions must match the value's Java type. AWK numbers holding an integral value reach String.format as Long, other numbers as Double, and text as String, so %d requires an integral value and %f a floating-point one; a mismatch raises Java's IllegalFormatException. Input-derived values are passed as plain strings, and uninitialized variables as Java null (which Formatter renders as "null").

  • Constructor Details

    • JavaStringFormatAwkSink

      public JavaStringFormatAwkSink(OutputStream outputStream)
      Creates a sink backed by an OutputStream.
      Parameters:
      outputStream - stream that should receive AWK output
    • JavaStringFormatAwkSink

      public JavaStringFormatAwkSink(OutputStream outputStream, Locale locale)
      Creates a sink backed by an OutputStream.
      Parameters:
      outputStream - stream that should receive AWK output
      locale - locale used for formatting
    • JavaStringFormatAwkSink

      public JavaStringFormatAwkSink(PrintStream printStream)
      Creates a sink backed directly by a PrintStream.
      Parameters:
      printStream - stream that should receive AWK output
    • JavaStringFormatAwkSink

      public JavaStringFormatAwkSink(PrintStream printStream, Locale locale)
      Creates a sink backed directly by a PrintStream.
      Parameters:
      printStream - stream that should receive AWK output
      locale - locale used for formatting
  • Method Details

    • sprintf

      public String sprintf(String convfmt, String format, Object... values)
      Formats with Java's standard String.format(Locale, String, Object...) instead of AWK's rules. convfmt is not used: number-to-string conversion follows the Java conversion in the format string.
      Overrides:
      sprintf in class AwkSink
      Parameters:
      convfmt - number-to-string conversion format (CONVFMT), unused
      format - Formatter-style format string
      values - arguments supplied after the format string
      Returns:
      formatted text