locked
How to get text between a range of variable length? RRS feed

  • Question

  • Example:

    WarehouseName ="NZT122" Product list
    Rice
    Corn
    Wheat
    Apple
    Peach
    Plum
    (Number of results = 6)
    WarehouseName ="AMY875" Product list
    Rice
    Corn
    Wheat
    Apple
    barley
    (Number of results = 5)

    Desired result

    NZT122
    Rice
    Corn
    Wheat
    Apple
    Peach
    Plum
    AMY875
    Rice
    Corn
    Wheat
    Apple
    barley

    In this example, is it possible to use -context X,0 where x is the Number of results value?

    Thanks for your help!

    Sunday, December 10, 2017 2:00 PM

All replies

  • You need to post your code and ask a clear question.

    "Context" is line oriented.   A RegEx could help to do this if the text is a predictable pattern.

    It can be done in a loop that detects "Warehouse" and accumulates until the next match.


    \_(ツ)_/


    • Edited by jrv Sunday, December 10, 2017 2:09 PM
    Sunday, December 10, 2017 2:07 PM
  • The following video might help you:

    Sophisitcated Techniques of Plain Text Parsing

    Good luck!


    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Sunday, December 10, 2017 2:40 PM
    Sunday, December 10, 2017 2:38 PM
  • The reason "context"  won't work is because it requires a fixed value in advanced.

    The video is excellent and covers almost all useful techniques.  It does not appear to cover this requirement exactly although it is implied.

    As an example:

    $txt = @'
    WarehouseName ="NZT122" Product list
    Rice
    Corn
    Wheat
    Apple
    Peach
    Plum
    (Number of results = 6)
    WarehouseName ="AMY875" Product list
    Rice
    Corn
    Wheat
    Apple
    barley
    (Number of results = 5)
    '@ -split "`n"
    
    $txt | 
        ForEach-Object{
            ($_ -replace 'WarehouseName ="(.*)" Product list', '$1') -replace '\(Number of results = \d+\)' 
        }


    \_(ツ)_/

    Sunday, December 10, 2017 7:50 PM
  • J.,

    I didn't expect you to see it but as always - your solution is simple, efficient and fits absolutely beautifully the requirement.  Great.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Sunday, December 10, 2017 9:23 PM
  • Thanks.  I have been parsing data for data warehousing and BI for decades. I always choose the simplest method that matches the data.

    The video link is a good intro to this topic.  Glad you found it.  I will post it to others as needed. The "extract by example" tools I have used for years.  MS Labs did a nice job on that tool.

    Here is a link to a simple example: https://1drv.ms/f/s!AjiiPtIUqzK_g4krUHpDWAENPv9WnQ

    The underlying tools are called "FlashExtract" .  The files have links to more info.


    \_(ツ)_/

    Sunday, December 10, 2017 9:29 PM
  • The real case is:


    MML Command-----LST GCELL:IDTYPE=BYNAME,BTSNAME="GLAA2";
    NE : BSCLDAC
    Report : +++    VIZO        2017-10-27 14:39:38 DST
    O&M    #7730649
    %%/*1879254753 MML Session=1509122378*/LST GCELL: IDTYPE=BYNAME,BTSNAME="GLAA2";
    RETCODE = 0  Execution succeeded.
    List GSM Cell at BSC
    --------------------
     Cell Index  Cell Name  Freq. Band  MCC  MNC  Cell LAC      Cell CI        NCC 
     449         ILAA1-1    DCS1880     724  06   H'0413(1043)  H'2DCD(11725)  5   
     453         ILAA1-2    DCS1880     724  06   H'0413(1043)  H'2DCE(11726)  5   
     454         ILAA1-3    DCS1880     724  06   H'0413(1043)  H'2DCF(11727)  6   
    (Number of results = 3)


    ---    END


    MML Command-----LST GCELL:IDTYPE=BYNAME,BTSNAME="GCTA1";
    NE : BSCCBRB
    Report : +++    BSCCBRB        2017-10-27 14:39:38 DST
    O&M    #5295260
    %%/*1879254752 MML Session=1509122378*/LST GCELL: IDTYPE=BYNAME,BTSNAME="GCTA1";
    RETCODE = 0  Execution succeeded.
    List GSM Cell at BSC
    --------------------
     Cell Index  Cell Name  Freq. Band  MCC  MNC  Cell LAC      Cell CI        NCC 
     31          GCTA1-1    PSM850      724  06   H'0BE1(3041)  H'9EAD(40621)  2   
     32          GCTA1-3    PSM850      724  06   H'0BE1(3041)  H'9EAF(40623)  2   
     228         GCTA1-2    PSM850      724  06   H'0BE1(3041)  H'9EAE(40622)  4   
    (Number of results = 3)


    The desired result is:


    GLAA2:
     449         ILAA1-1    DCS1800     724  06   H'0413(1043)  H'2DCD(11725)  5   
     453         ILAA1-2    DCS1800     724  06   H'0413(1043)  H'2DCE(11726)  5   
     454         ILAA1-3    DCS1800     724  06   H'0413(1043)  H'2DCF(11727)  6
    GCTA1:
     31          GCTA1-1    PSM850      724  06   H'0BE1(3041)  H'9EAD(40621)  2   
     32          GCTA1-3    PSM850      724  06   H'0BE1(3041)  H'9EAF(40623)  2   
     228         GCTA1-2    PSM850      724  06   H'0BE1(3041)  H'9EAE(40622)  4

     


    I'll adjust your code with real case. Thanks also for the study material!

    • Edited by jrv Monday, December 11, 2017 1:15 PM Fixed format
    Monday, December 11, 2017 1:06 PM
  • Follow the instructions in the above links to learn how to parse txt.  We will not do this for you.

    As you can see your real issue is quite different from the original post but my suggested method can be made to work.


    \_(ツ)_/


    • Edited by jrv Monday, December 11, 2017 1:17 PM
    Monday, December 11, 2017 1:14 PM