XTRAN Example — Enable Sorting of DSV Data

Scenario — you have some delimiter separated value (DSV) data that you want to sort using a text sorting utility.  Your task is complicated by the following issues:

XTRAN to the rescue!

The following example uses an XTRAN rules file comprising 300 non-comment lines of XTRAN's rules language ("meta-code") to prepare DSV data for sorting.  The original rules took just over an hour to write and about an hour to debug.  (That's right, just over two hours total!)

This is an example of XTRAN's ability to automate the manipulation of data as well as code.

You tell the rules, via environment variable values:

The rules then prepend, to each DSV data line, the fields on which to sort:

You can then use a "dumb" sorting utility to do the sorting, followed by a pass through a text processing utility such as sed to remove the prepended sort field copies and separating token, restoring the data to its original form (but now sorted as requested).

We provide, with XTRAN, a BASH script to automate this process.

In the example below, we sort some DSV data:

The input to and output from XTRAN are untouched.

How can such powerful and generalized data manipulation be automated in about 3 hours and only 300 lines of rules?  Because there is so much capability already available as part of XTRAN's rules language. The rules used for this example take advantage of the following functionality provided by that rules language:


Process Flowchart

Here is a flowchart for this process, in which the elements are color coded:

data flowchart

Input to XTRAN:

# dsvsrt.in -- Input file for demonstrating sorting of DSV data
#
def!abc12!RCD3!1,230!end3
abc!xyz1234!Rcd1!25!end1
abc!xyz1!rcd2!1,432!end2
xyz!xyz1!rcd2!1,230!end2
def!xyz1234!RCD1!25!end1
abc!xxyz!Rcd1!123,456!end3
#
# End of dsvsrt.in


Output from XTRAN, as processed by the sort utility and the text utility:

abc!xxyz!Rcd1!123,456!end3
abc!xyz1!rcd2!1,432!end2
xyz!xyz1!rcd2!1,230!end2
def!abc12!RCD3!1,230!end3
abc!xyz1234!Rcd1!25!end1
def!xyz1234!RCD1!25!end1