X12 Unwrapper

X12 Unwrapper

3
NewbieNewbie
3

    Jun 13, 2013#1

    By default X12 text is on a single line. I've gotten very good at hitting Ctrl+H, Ctrl+R, Enter, Ctrl+H. However I would like to create a macro to automate this task. I'm not sure where to start, but here's my pseudo code, I appreciate any and all help.

    // get the character in position 106 << segmentDelimiter

    // If segmentDelimiter is not a letter, number, or white space continue (this will typically be a ~)

    // If character in postition 107 is not a white space continue (whitespace at 107 means the transaction has already been unwrapped)

    // Replace segmentDelimiter with segmentDelimiter + carriage return + line feed

    6,675585
    Grand MasterGrand Master
    6,675585

      Jun 16, 2013#2

      I'm quite sure that a single regular expression Find + Replace All can do the reformatting task. But I have troubles understanding the requirements of the task.

      Would you please create two examples files, pack them with ZIP or RAR into an archive and upload this ZIP/RAR archive as attachment to your next post. The first example file should contain the input example for the macro - the X12 line(s). The second file should contain the wanted output of the macro for the first file. That would make it much easier for us to understand the requirements and code the macro.

      For example the Perl regular expression search string (.{105}[^\w\s])(?!\n) and the replace string \1\r\n used in a Replace All could do the job.

      (...) marks (tags) the string found by the expression inside the round brackets for back referencing (re-use) which is done here in the replace string by \1

      .{105} finds exactly 105 characters not including carriage return and line-feed.

      [^\w\s] is a negative character set definition for a single character. The 106th character should not be a
      • \w ... a word character which includes all letters in all languages in any case (not just a-z), all digits and the underscore
      • \s ... a whitespace character which includes space, horizontal tab, carriage return, line-feed and some more whitespace characters from which you most likely never heard and we therefore can expect not existing in your files.
      (?!\n) is a negative lookahead for the next character which is evaluated, but not selected. After 106 characters there should be no line-feed. (This is correct even for DOS terminated files although the next character stored on disk is a carriage return.) Lines with exactly 106 characters and then the line termination follows are ignored because of this negative lookahead.

      The replace string replaces the 106 characters matched by the same 106 characters (= change nothing) and adds a carriage return + line-feed.

      Therefore this Perl regular expression Replace All inserts a DOS line termination after 106 characters if the line is longer than 106 characters and the 106th character is not a letter, digit or whitespace character.

      1
      NewbieNewbie
      1

        Dec 18, 2013#3

        You can use the replace function...have it find ~ and then replace with ~^p.
        After you have that setup, just hit the replace all button and you are good to go.

        3
        NewbieNewbie
        3

          Apr 24, 2014#4

          I'm back at this. I altered your regex a little
          (?<=ISA.{97}[01][^\w\s][PT][^\w\s]{2})([^\w\s])(?!\n)

          The problem I'm having is that it only replaces the first instance of the 106th character.

          This:

          Code: Select all

          ISA*03*          *00*          *ZZ*000000000      *ZZ*000000000      *130305*1701*|*00501*000001466*0*P*:~GS*HC*000000000*000000000*20130305*1701*2466*X*005010X222A1~ST*837*0001*005010X222A1~
          
          Should become:

          Code: Select all

          ISA*03*          *00*          *ZZ*000000000      *ZZ*000000000      *130305*1701*|*00501*000001466*0*P*:~
          GS*HC*000000000*000000000*20130305*1701*2466*X*005010X222A1~
          ST*837*0001*005010X222A1~

          6,675585
          Grand MasterGrand Master
          6,675585

            Apr 24, 2014#5

            Do you really know what the Perl regular expression (?<=ISA.{97}[01][^\w\s][PT][^\w\s]{2})([^\w\s])(?!\n) means?

            It looks like you do not as otherwise you would not wonder why you do not get the expected result. I could explain the expression, but I think this is a waste of time as the expression using a very large lookbehind will be never useful for the second ~ in your example.

            According to suggestion of EDIPM my suggestion is now to use the Perl regular expression ~(?!\r) as search string and ~\r\n as replace string to find ~ not being at end of a DOS terminated line and insert a carriage return + line-feed after tilde character.

            With the UltraEdit regular expression not supporting a non matching negative lookahead, you can search for ~^([~^r]^) and use as replace string ~^p^1 which does the same as the Perl regular expression replace.
            Best regards from an UC/UE/UES for Windows user from Austria

            1
            NewbieNewbie
            1

              May 16, 2014#6

              This is the macro I've been using. It also allows you to reverse the delimiter change. I bind it to Ctrl + ~ for quick use.

              Code: Select all

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOn
              Clipboard 7
              GotoLine 1 106
              IfCharIs 13
              Find "^p"
              Replace All "^c"
              Else
              IfCharIs 10
              Find "^p"
              Replace All "^c"
              Else
              GotoLineSelect 0 107
              Copy
              Find "^c"
              Replace All "^p"
              EndIf
              EndIf
              Clipboard 0
              Top
              

              3
              NewbieNewbie
              3

                Dec 15, 2014#7

                Very nice trumaine!