Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Favourite One-liners?

by etcshadow (Priest)
on Jun 27, 2005 at 23:58 UTC ( [id://470422]=note: print w/replies, xml ) Need Help??


in reply to Favourite One-liners?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://470422]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found