Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Favourite One-liners?

by etcshadow (Priest)
on Jun 27, 2005 at 23:58 UTC ( #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


Comment on Re: Favourite One-liners?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2015-07-07 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (91 votes), past polls