Text driver

Parse text files with regex or generate formatted reports.

Configure the connection

Use driver="text". No additional runtime dependencies are required. Relative url paths resolve from the ETL file. Omit the URL to read standard input or write standard output.

Set encoding=UTF-8 for portable results. The default is the system charset. The driver trims leading and trailing whitespace by default. Use trim=false when whitespace matters, such as in fixed-width records. Set eol to change the output line ending from LF.

Extract records with a regular expression

The driver applies Java regular expressions one line at a time. A match is available as $column0, and captured groups are available as $column1, $column2, and so on. Use anchors to match the entire line. Patterns cannot span input lines.

Save this fixed-format input as legacy.txt. The first line, EXPORT BATCH, is a title. The pattern matches only lines beginning with ORDER and captures the order ID and customer name.

EXPORT BATCH
ORDER 0042 Ada Lovelace
ORDER 0087 Grace Hopper

Save the ETL file beside it as extract-text.etl.xml and run scriptella.sh extract-text.etl.xml.

<?xml version="1.0"?>
<!DOCTYPE etl SYSTEM "http://scriptella.org/dtd/etl.dtd">
<etl>
    <connection id="input" driver="text" url="legacy.txt">
        encoding=UTF-8
    </connection>
    <connection id="output" driver="text" url="orders.txt">
        encoding=UTF-8
    </connection>
    <query connection-id="input">
        ^ORDER ([0-9]{4}) (.+)$
        <script connection-id="output">Order $column1 belongs to ${column2}.</script>
    </query>
</etl>

The resulting orders.txt contains the matching records.

Order 0042 belongs to Ada Lovelace.
Order 0087 belongs to Grace Hopper.

Generate a formatted report

A Text script expands $variable and ${variable} references. A script nested in a SQL query can use the query's column names. Configure format.<name>.* properties for numbers, dates, timestamps, locale and padding.

Save this as report-text.etl.xml and run scriptella.sh report-text.etl.xml.

<?xml version="1.0"?>
<!DOCTYPE etl SYSTEM "http://scriptella.org/dtd/etl.dtd">
<etl>
    <properties>
        team=Operations
        total=42
    </properties>
    <connection driver="text" url="report.txt">
        encoding=UTF-8
        format.total.pad_left=6
    </connection>
    <script>
        Monthly report for $team
        Total | ${total} |
    </script>
</etl>

The resulting report.txt pads the total to six characters.

Monthly report for Operations
Total |     42 |

Structured output

Text substitution inserts values as-is, without escaping delimiters, quotes or other special characters. Use the CSV driver for delimited files and the JSON driver for JSON or JSON Lines.

See the Text driver Javadoc for query and script syntax and the formatting reference for connection properties.