none
how to use powershell cmdlet to format a string from mulitple lines?

    Question

  • Using the sample data below, I'd like to extract the text between DESC and END so that it appears as 1 line, like:

    DESC BUY size LARGE object Orange blah blah hello. END

    DESC BUY small scale electronics size Mini with object MP3 on today lah blah hello. END

    Sample Data:

    ID      1001
    LOC    Home
    DESC    BUY size LARGE object Orange
        blah blah hello.
    END
    ID      1002
    LOC    Shop
    DESC    BUY electronics size Mini with object
        MP3 on today lah blah
        hello.
    END

    It seems that I can replace mulitple spaces but attempting to replace carriage returns are ignored.  Additionally, I haven't figured out how to get Select-String to operate on data from DESC to END, typically it only operates on the current line.  Why "END" is important is because I want the carriage return after "END" but ignored in the lines preceding it.    This would be the first pass through.  The end goal is to extract the values associated with size and object which values aren't always on the same line; hence repackaging the statements so they appear on the same line.

    Any help would be much appreciated.

    Tuesday, November 13, 2012 6:28 PM

Answers

  • How about this:

    get-content file.txt |
     foreach {
      switch -Regex ($_){
       ^DESC {$test=$true;$temp = $_}
       ^END  {$temp += $_;$temp;$test=$false}
       Default {
                 If ($test){$temp += $_}
                 else {$_}
                 }
        }
    } set-content newfile.txt


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, November 13, 2012 7:33 PM
    Moderator

All replies

  • How about this:

    get-content file.txt |
     foreach {
      switch -Regex ($_){
       ^DESC {$test=$true;$temp = $_}
       ^END  {$temp += $_;$temp;$test=$false}
       Default {
                 If ($test){$temp += $_}
                 else {$_}
                 }
        }
    } set-content newfile.txt


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, November 13, 2012 7:33 PM
    Moderator
  • Hi,

    Did mjolinor's script work for you? Do you still need help with this question?

    Bill

    Thursday, December 20, 2012 10:54 PM
    Moderator
  • I appreciate this thread looks to be abandoned... I thought I would post this alternative method in case anyone is looking...

    $sampledata=@"
    ID      1001
    LOC    Home
    DESC    BUY size LARGE object Orange 
       blah blah hello.
    END
    ID      1002
    LOC    Shop
    DESC    BUY electronics size Mini with object
        MP3 on today lah blah 
       hello.
    END
    "@
    
    ([regex]::Matches($sampledata,'DESC.+?END',"SingleLine,IgnoreCase")).Groups | Select @{l='Value';e={$_.Value -replace "\s{2,}"," "}}
    
    Result:
    
    Value 
    ----- 
    DESC BUY size LARGE object Orange blah blah hello. END
    DESC BUY electronics size Mini with object MP3 on today lah blah hello. END


    Admiral Ackbar says...

    Friday, December 21, 2012 2:06 AM
  • Suppose the strings 'DESC' or 'END' appeared somewhere within that (apparently arbitrary) text between the intended anchors?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, December 21, 2012 2:17 AM
    Moderator
  • Suppose the strings 'DESC' or 'END' appeared somewhere within that (apparently arbitrary) text between the intended anchors?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    If that were the case, then your solution would be the wisest choice :-)


    Admiral Ackbar says...

    Friday, December 21, 2012 2:24 AM
  • Soon it will be July.  You will still not have solved this puzzle.


    ¯\_(ツ)_/¯

    Friday, December 21, 2012 3:07 AM
  • Soon it will be July.  You will still not have solved this puzzle.


    ¯\_(ツ)_/¯


    Are you implyng I can't solve this puzzle jrv? Or just that it would take a long time?

    Admiral Ackbar says...

    Friday, December 21, 2012 3:14 AM
  • Soon it will be July.  You will still not have solved this puzzle.


    ¯\_(ツ)_/¯


    Are you implyng I can't solve this puzzle jrv? Or just that it would take a long time?

    Admiral Ackbar says...


    We are into the second month of this. July is not that far away.

    ¯\_(ツ)_/¯

    Friday, December 21, 2012 3:21 AM
  • The puzzle, as posted, appears to have two solutions already.

    To the OPs satisfaction?

    I guess we'll never know.

    You seem to have another solution in mind jrv?


    Admiral Ackbar says...

    Friday, December 21, 2012 4:02 AM
  • I had a washing machine like that once.

    The timer was sticky, and every once in a while it would get stuck on Agitate ;).

    @RiffyRiot-

    Consider:

    $regex = '(?ms)^DESC\s.+?^END\s*$'
    [regex]::Matches($text,$regex) |
      select -Expand Value


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, December 21, 2012 4:08 AM
    Moderator
  • I had a washing machine like that once.

    The timer was sticky, and every once in a while it would get stuck on Agitate ;).

    @RiffyRiot-

    Consider:

    $regex = '(?ms)^DESC\s.+?^END\s*$'
    [regex]::Matches($text,$regex) |
      select -Expand Value


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    hehe... Agitate. I like it

    Thanks mjolinor, I understand what you've done there.

    I was hoping jrv would turn his considerable skills to the 'puzzle' in question, and blow our minds with something elegant and powerful, using a technique us mortals would never have considered...


    Admiral Ackbar says...

    Friday, December 21, 2012 4:22 AM
  • I was hoping jrv would turn his considerable skills to the 'puzzle' in question, and blow our minds with something elegant and powerful, using a technique us mortals would never have considered...


    Admiral Ackbar says...


    He might yet......

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, December 21, 2012 4:26 AM
    Moderator
  • no - I was just " agitating".  I think Ron got it best. Of course you could still object.

    Hey.  How about those Mayans?


    ¯\_(ツ)_/¯

    Friday, December 21, 2012 4:42 AM
  • no - I was just " agitating".  I think Ron got it best. Of course you could still object.

    Hey.  How about those Mayans?


    ¯\_(ツ)_/¯

    I am disappoint... I was hoping for something I could tell my grandkids about ;-)

    End of the Mayan calendar? Which Shakespeare play is that... Much Ado About Nothing?

    No objection.. I think both solutions are adequate considering the information provided.


    Admiral Ackbar says...

    Friday, December 21, 2012 4:58 AM
  • Archeologist from Central American concur that the new excavation has found the "fixed" calendar.  The EOT is now moved forwards by 8000 years.  The Mayans found a small arithmatic error some years later and fixed it.  Early archaelogists missed that bit until recently.


    ¯\_(ツ)_/¯

    Friday, December 21, 2012 5:06 AM
  • NASA did an interesting write up on the subject... http://www.nasa.gov/topics/earth/features/2012.html

    Admiral Ackbar says...

    Friday, December 21, 2012 5:14 AM
  • To simplify for my audience...the world wil still be here after tomorrow so don't start celebrating now.  You still have to buy a xmas gift for you mother-in-law this year.  Live with it!

    This is perfect as t'would be:

    The lunatic, the lover, and the poet
    Are of imagination all compact.
    One sees more devils than vast hell can hold,
    That is, the madman: the lover, all as frantic,
    Sees Helen's beauty in a brow of Egypt:
    The poet's eye, in a fine frenzy rolling,
    Doth glance from heaven to earth, from earth to heaven;
    And as imagination bodies forth
    The forms of things unknown, the poet's pen
    Turns them to shapes, and gives to airy nothing
    A local habitation and a name.
    Such tricks hath strong imagination,
    That if it would but apprehend some joy,
    It comprehends some bringer of that joy;
    Or in the night, imagining some fear,
    How easy is a bush supposed a bear!

    The form of things unknown....

    And so we leave you with this lament:

    If we shadows have offended,
    Think but this, and all is mended,
    That you have but slumbered here
    While these visions did appear.

    Good night all.


    ¯\_(ツ)_/¯

    Friday, December 21, 2012 5:17 AM