OH! And how can I forget
this puppy. I know there probably aren't that many monks out there who fool around with X12 data files very often... but it is super sweet.
I actually built a little shell-script around it as follows:
#!/bin/sh
# since "perl -p ... -n ..." will be treated by perl as just
# "perl -p ... ..." (not allowing the -n to override the -p)
# we have to scan through any switches on the command line,
# so that commands like "x12cat -ne 'print if /^PLB/' ..."
# will work as expected... but still allow an implicit "-p"
# if no "-n" is specified. Granted this isn't perfect...
# But this isn't so bad, because an explicit "-p" can override
# this.
looptype=""
for i do
case "$i" in
-m*) ;; # -m and -M start command-line "use" direct
+ives,
-M*) ;; # "n"s in them should be ignored
-*n*) looptype=n;;
-h) echo 'x12cat: "magic" x12 spooler wrap
+per around command-line perl.
-splits files by segment terminator
-auto sets $f and $c to (regex-escaped) field and component separato
+rs
-auto sets the autosplit to the field separator (for the -a autospli
+t)
-allows -pe or -ne operations on file (or none at all to just cat th
+e file)'; exit;;
-*) ;;
*) break;;
esac
done
if [ "x$looptype" = "x" ]; then
looptype=p
fi
perl -l -$looptype -e 'BEGIN{$SIG{PIPE}="exit"; $|=1; $/=\108; ($f,$c,
+$/)=(<> =~ /^ISA(.).{100}(.)(.?\r?\n?)/); ($f,$c)=("\Q$f","\Q$c"); se
ek ARGV,0,0; $.=0}' -F'/$f/' "$@"
Makes me so happy. And then (if it isn't clear), I compose one-liners off of that wrapper (called x12cat). Like:
x12cat -ne 'print if /^PLB/' file.x12 # grep for PLB segments
x12cat -ane '$amt{$F[1]} += $F[2] if /^AMT/; END{ print "$_: $amt{$_}"
+ for sort {$amt{$b}<=>$amt{$a}} keys %amt }' file1.x12 file2.x12 ...
+# print sorted break down, by amount descending, of AMT code-types
Now if that's not some practical extraction and reporting... I don't know what is! (If you knew what the X12 data language was, you'd think that was pretty damn cool, I guarantee it).
------------
:Wq
Not an editor command: Wq
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.