Tapatalk

Separating multiline block with variable amount of lines

Separating multiline block with variable amount of lines

2
NewbieNewbie
2

    May 22, 2020#1

     I have below example file where I only need to see the text blocks starting with 100~|~ABCD123 and ending with the 310~ and its line content.

    Code: Select all

    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Eric~|Teststreet 1~|Testvillage~|~|~|~|~| 012 345 67 89~|
    310~|Some information A1234-1
    310~|Some information A1234-2
    310~|Some information A1234-3
    310~|Some information A1234-4
     
    100~|~|DEFG2345
    200~|~|A5678~|
    300~|~|~|~|~|Jack~|Betasquare 2~|Happyspace~|~|~|~|~| 022 335 57 77~|
    310~|Some information A5678-1
    310~|Some information A5678-2
     
    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Andrew~|Roundabout 7~|Cloudy~|~|~|~|~| 033 444 55 66~|
    310~|Some other information A1234-1-1
    310~|Some other information A1234-2-1
    310~|Some other information A1234-3-1
     
    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Dennis~|SunShine 7~|Sunvillage~|~|~|~|~| 098 765 43 21~|
    310~|Some other information A1234-1-1
    310~|Some other information A1234-2-1
     
    100~|~|HIJK2345
    200~|~|A5678~|
    300~|~|~|~|~|Jack~|Pencil 2~|markerland~|~|~|~|~| 055 765 12 98~|
    310~|Some information A5678-1
    310~|Some information A5678-2
    In here I am searching for the multi-line pattern:

    Code: Select all

    100~|~|ABCD123
    200~|~|A1234~|
    300~|*
    310~|*
    It seems like all items get found (3), but the last 310~ field is not always an equal amount, thus only the first line of many is found.
    How do I separate these complete text blocks from the file (to a new file) with only below result?

    Code: Select all

    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Eric~|Teststreet 1~|Testvillage~|~|~|~|~| 012 345 67 89~|
    310~|Some information A1234-1
    310~|Some information A1234-2
    310~|Some information A1234-3
    310~|Some information A1234-4
    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Andrew~|Roundabout 7~|Cloudy~|~|~|~|~| 033 444 55 66~|
    310~|Some other information A1234-1-1
    310~|Some other information A1234-2-1
    310~|Some other information A1234-3-1
    100~|~|ABCD123
    200~|~|A1234~|
    300~|~|~|~|~|Dennis~|SunShine 7~|Sunvillage~|~|~|~|~| 098 765 43 21~|
    310~|Some other information A1234-1-1
    310~|Some other information A1234-2-1

    6,685587
    Grand MasterGrand Master
    6,685587

      May 22, 2020#2

      A Perl regular expression search string to find the text blocks of interest is: ^100~\|~\|ABCD123.*\r?\n(?:(?!310~\|).++\r?\n)*?(?:310~\|.*\r?\n)+
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        May 22, 2020#3

        Wow :) That looks really cool...
        I am not familiar with Perl. Does this need to have separate program installed under Windows? I was doing this on my business laptop and am not able to install any software myself.

        6,685587
        Grand MasterGrand Master
        6,685587

          May 22, 2020#4

          UltraEdit has built-in the Boost Perl regular expression find/replace engine. So you don't need an additional program. All you need is UltraEdit.

          With caret (= text cursor) at top of the file, open the regular Find dialog by pressing Alt+F3 or twice Ctrl+F and copy and paste that search string (without line ending) into Find what edit field with replacing everything currently displayed there. Then click on the gearwheel button if you don't see the advanced find options. Check the option Regular expressions and select Perl as regular expression engine to use. Check also the option List lines containing string before clicking on button Next.

          There should be now displayed the window with title Find String List. Click with secondary (right) mouse button into this window and click with primary (left) mouse button on context menu item Copy results to new file. You have to click on Copy to clipboard, press Ctrl+N to create a new file and paste the found blocks with Ctrl+V into the new file on using an older version of UltraEdit which does not have the Find String List context menu item Copy results to new file.

          It is also possible to write an UltraEdit macro or an UltraEdit script to get those blocks copied into a new file more automated if this task needs to be done periodically. In this case let us know how large is the file usually to write the macro/script as most efficient as possible. It makes a difference if a file has just up to 4 MB or hundreds of MBs or even several GBs.
          Best regards from an UC/UE/UES for Windows user from Austria

          19176
          MasterMaster
          19176

            May 22, 2020#5

            Hi Joostman & Mofi,

            Just a little optimalization if I may
            ^100~\|~\|ABCD123.*\r?\n(?:(?!310~\|).++\r?\n)*+(?:310~\|.*\r?\n)+

            BR, Fleggy