Remove Leading Zeros

Clovertech Forums Cloverleaf Remove Leading Zeros

  • Creator
    Topic
  • #122437
    bso its
    Participant

      I have a flat file (see attached)

      I need to remove the leading zeros at the start of the line only. ie 00008 to  8

      I can remove the leading zeros via a tcl proc but it wont read the message in.

      proc removeLeadingZerosPerLine {DATA args} {

      echo “RUNNING PROC VERSION: PRE-WRITE SIGNATURE”
      echo “removeLeadingZerosPerLine: START”
      echo “Original DATA:\n$DATA”

      set message $DATA
      set outLines {}
      set lineNum 0

      foreach line [split $message “\n”] {
      incr lineNum
      set line [string trimright $line “\r”]

      echo “Line $lineNum BEFORE: ‘$line'”

      if {[string match “*UKBTSSTART*” $line] ||
      [string match “*UKBTSSTOP*” $line]} {
      set line “[string range $line 4 end]”
      } elseif {[regexp {^0000[0-9]} $line]} {
      set line “[string index $line 4][string range $line 5 end]”
      }

      lappend outLines $line
      }

      set newMessage [join $outLines “\r\n”]
      echo “FINAL transformed DATA:\n$newMessage”
      echo “removeLeadingZerosPerLine: END”

      return $newMessage
      }

      I can remove via a translation  – using xlateStrTrimLeft 0000 but it will only remove the zeros from the first line and wont read in the other lines (delimited by <CR><LF> – which need to remain.)

      (I have create the translation using FRL and VRL)

      Any help appreciated

    Viewing 1 reply thread
    • Author
      Replies
      • #122438
        David Barr
        Participant

          How are you calling the TCL proc?

           

        • #122439
          Jason Russell
          Participant

            A lot of this is going to depend on how you’re pulling the flat file in. Are you reading the file by line or the entire file at once? If you’re reading by line and not EOF you should be able to have a simple TPS (or in your xlate) that just looks at the first element and simply either use xlateStrTrimLeft 0 (you only need one) or the full tcl: [string trimleft $var 0]

            If you’re pulling in the entire file, you can do this in a TPS before you xlate (if it’s only at the beginning of a line):

            set messageList [split $msg \n]

            foreach line $messageList {

            lappend newmsg [string trimleft $line 0]

            }

            set newmsg [join $newmsg \n]

            msgset $mh $newmsg

            Considering you have the newline last you shouldn’t need to split on both \r and \n, just split and join on \n, that should preserve it all.

             

            This also assumes you’re using the TPS template from within the GUI to build the TPS with the correct arguments. You would put the script in the prexlate tps script box. Just insert that code in the ‘run’ portion of the switch statement and you should be good to go.

        Viewing 1 reply thread
        • You must be logged in to reply to this topic.