x12 Add trailer header to separate files

Homepage Clovertech Forums Read Only Archives Cloverleaf Cloverleaf x12 Add trailer header to separate files

  • Creator
    Topic
  • #51027
    Femina Jaffer
    Participant

    Hi All,

    I have written tcl code to read an x12 file, which breaks each raw file read into separate files and adds the ISA header to each file it breaks.  I now need to add the Trailer to each file.  I am having a very difficult time to get this to work.  However, I am able to extract the IEA hdr and store it in a variable.

    The code is listed below…please advise.

       keylget args MODE mode               ;# Fetch mode

       set dispList {} ;# Nothing to return

       switch -exact — $mode {

           start {

               # Perform special init functions

       # N.B.: there may or may not be a MSGID key in args

           }

           run {

       # ‘run’ mode always has a MSGID; fetch and process it

               keylget args MSGID mh

         set msg [msgget $mh]

               # set dir /hci/quovadx/qdx5.5/integrator/trlprod/FTP/EOB/STRIP/IN

               set dir /hci/quovadx/qdx5.5/integrator/testdev_res/

               set count 0

       set segCount 0

               set segID “”

               set elSep [string index $msg 104]

               set ISAHdr [string range $msg 0 [string first “${elSep}” $msg]]

               set splitMsg [split $msg ~]

               set IEAFtr [lregexp $splitMsg {^IEA}]

                   

                 foreach seg $splitMsg {

         if {$segCount > 0} {

             set preSegInd $segID

                             

         }

                   set segID [string range $seg 0 2]

       incr segCount +1

                     switch -exact $segID {

                       ISA     {

                                 lappend outMsg $seg }

                               

                       GS*     {  if {$count > 0} {

                              set outFile [open $dir/PHP$outFileName.dat w]

                      set outMsg “[join $outMsg ~]~”

                              puts $outFile $outMsg

                      flush $outFile

                      set outMsg “”

                                      lappend outMsg $ISAHdr

                                      lappend outMsg $seg

                      incr count +1

                  } else {  

                                           lappend outMsg $seg

                           incr count +1

                         }

                                       

                  }

       

                       CAS      { if {$segID == “CAS”} {

                                    set oa_ind [lindex [split $seg *] 1]

                                    set oa_val [lindex [split $seg *] 2]

                              if {[cequal $oa_ind “OA”] && [cequal $oa_val “45”]} {

                                            regsub -all “OA” $seg “CO” nseg

                                            set seg $nseg

                                            lappend outMsg $seg

                                       } else { lappend outMsg $seg

                                         }

                                       } else { lappend outMsg $seg

                                         }

                                   }

               TRN      { set outFileName [lindex [split $seg *] 2]

                                  lappend outMsg $seg

               }

                       default  { lappend outMsg $seg

                                   

                        }

                     }

                 }        

                 if {$count != -1} {

                     set outFile [open $dir/PHP$outFileName.dat w]

                     set outMsg [join $outMsg ~]

                     puts $outFile $outMsg

                     flush $outFile

                 }

       lappend dispList “KILL $mh”

           }

    Thanks for any advise or feedback.

Viewing 6 reply threads
  • Author
    Replies
    • #68492
      Levy Lazarre
      Participant

      Femina,

      Have you been able to resolve this? If not, please please post a sample file for testing changes to your code.

      –Levy

    • #68493
      Femina Jaffer
      Participant

      Thank you Levy.  I have not yet figured this out yet, actually haven’t worked on it again until this morning.  

      I have attached the test file for you.  It is a *.dat file.  If you need to view it, you will need to open it in notepad.

      Thanks again for looking at this.

    • #68494
      Femina Jaffer
      Participant

      Forgot the attachment!

    • #68495
      Levy Lazarre
      Participant

      Hi Femina,

      Here is my updated version of your script. Please rename to .tcl and test it.

      You were on the right track. You needed a line of code to add the footer in two locations. I have also added an IEA section in the switch statement and recalculated the number of groups in the modified IEA footer. The “if” test at the end of the script is always true and can be eliminated.

      I hope this helps.

      –Levy

    • #68496
      Femina Jaffer
      Participant

      This looks really good and does exactly what I want.  Again, thank you so much for your help.

    • #68497
      Femina Jaffer
      Participant

      Sorry Levy, I spoke to soon.  The script is appending an extra footer to each output file.  Please advise?

    • #68498
      Femina Jaffer
      Participant

      Actually, in UNIX it appears to be twice. So I brought it up in Windows / Notepad – and the problem really is that the last file does not have the footer attached.  The script appears to be adding the footer to the first file and missing the footer in the last file.

Viewing 6 reply threads
  • The forum ‘Cloverleaf’ is closed to new topics and replies.

Forum Statistics

Registered Users
5,117
Forums
28
Topics
9,292
Replies
34,435
Topic Tags
286
Empty Topic Tags
10