Windows Vista Forums

are there tutorial on string processing with power-shell?
  1. #1


    light_wt Guest

    are there tutorial on string processing with power-shell?

    I am new to power-shell and find it hard to follow.



    Are there step-by-step tutorial showing how to use it for string processing?

    Thanks.

      My System SpecsSystem Spec

  2. #2


    RichS Guest

    RE: are there tutorial on string processing with power-shell?

    I don't know of a specific tutorial on string handling with PowerShell but
    what you can do is use PowerShell to help you learn.
    if you create a string variable and then put it through get-member you will
    see a list of the methods and properties for that string.

    $a = "abcdef"

    $a | get-member

    You can find information about the methods and properties here -
    http://msdn2.microsoft.com/en-us/lib...rs(vs.80).aspx

    If you have specific questions about string handling you can always post
    them on the forum


    Most of the PowerShell books that I've seen have something about using strings


    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "light_wt" wrote:

    > I am new to power-shell and find it hard to follow.
    >
    > Are there step-by-step tutorial showing how to use it for string processing?
    >
    > Thanks.


      My System SpecsSystem Spec

  3. #3


    Keith Hill [MVP] Guest

    Re: are there tutorial on string processing with power-shell?

    "light_wt" <lightwt@discussions.microsoft.com> wrote in message
    news:ADC309EF-BC30-4883-834F-2552556C6534@microsoft.com...
    >I am new to power-shell and find it hard to follow.
    >
    > Are there step-by-step tutorial showing how to use it for string
    > processing?
    >


    There are a number of books out on PowerShell. I really like WIndows
    PowerShell in Action. Perhaps you could ask your questions here and we'll
    attempt to answer them. Or is it the case that you don't know enough yet to
    know what questions to ask? BTW what exactly do you mean by string
    processing? Regex operations on strings, how to do variable substitution in
    a string, ...

    --
    Keith



      My System SpecsSystem Spec

  4. #4


    light_wt Guest

    Re: are there tutorial on string processing with power-shell?

    thanks keith and rich for jumping into it.

    I have several patterns that i like to parse.

    here is the first. A file with about 1000 lines of repeated patten which
    goes liked...

    ADD ...
    set ...
    DO ...
    execute ...

    ADD ...
    ..
    ..
    ..


    i want to break them up into multiple files where the block from "ADD to
    execute" will be in one file.

    thanks in advance for your couching.


    "Keith Hill [MVP]" wrote:

    > "light_wt" <lightwt@discussions.microsoft.com> wrote in message
    > news:ADC309EF-BC30-4883-834F-2552556C6534@microsoft.com...
    > >I am new to power-shell and find it hard to follow.
    > >
    > > Are there step-by-step tutorial showing how to use it for string
    > > processing?
    > >

    >
    > There are a number of books out on PowerShell. I really like WIndows
    > PowerShell in Action. Perhaps you could ask your questions here and we'll
    > attempt to answer them. Or is it the case that you don't know enough yet to
    > know what questions to ask? BTW what exactly do you mean by string
    > processing? Regex operations on strings, how to do variable substitution in
    > a string, ...
    >
    > --
    > Keith
    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    Keith Hill [MVP] Guest

    Re: are there tutorial on string processing with power-shell?

    "light_wt" <lightwt@discussions.microsoft.com> wrote in message news:BAA301D0-EE37-484B-9FC1-2B5C03EB7D3D@microsoft.com...
    > thanks keith and rich for jumping into it.
    >
    > I have several patterns that i like to parse.
    >
    > here is the first. A file with about 1000 lines of repeated patten which
    > goes liked...
    >
    > ADD ...
    > set ...
    > DO ...
    > execute ...
    >
    > ADD ...
    > .
    > .
    > .
    >
    >
    > i want to break them up into multiple files where the block from "ADD to
    > execute" will be in one file.


    Try this:

    # Read file contents - this is created as array of strings
    $text = get-content c:\temp\data.txt

    # Join the array of strings into a single string, necessary
    # because we want to use a regex that spans lines
    $text = [string]::join("`n", $text)

    # Create a regex that uses singleline (. matches all) and
    # multiline (enhanced anchor mode) to search for
    # text between ^ADD and another ^ADD (or end of string).
    $regex = [regex]'(?sm)^ADD.*?(?=(^ADD|\Z))'

    # Use the System.Text.RegularExpressions.Regex.Matches static method
    $regex.Matches($text) | foreach {$i = 0}{$i++; $_.Value.Trim() > "foo${i}.txt"}

    --
    Keith

      My System SpecsSystem Spec

  6. #6


    light_wt Guest

    Re: are there tutorial on string processing with power-shell?

    this is cool, keith.

    what i like to do next is to create the filename based on a token in the ADD
    block, which is always after the 4thkeyword. That 4th keyword is RESERVE

    ADD 1stkeyword 2ndvar 3rdkeyword RESERVE UserSpecifiedFileName123
    set ...
    DO ...
    execute ...

    ADD 1stkeyword 2ndvar 3rdkeyword RESERVE UserSpecifiedFileNameBBC
    ..
    ..
    ..

    So, would the code be something liked...
    $FileName=OneOfUserSpecifiedFileName
    $regex.Matches($text) | {$_.Value.Trim() > "$FileName.txt"}
    ???



      My System SpecsSystem Spec

  7. #7


    Keith Hill [MVP] Guest

    Re: are there tutorial on string processing with power-shell?

    "light_wt" <lightwt@discussions.microsoft.com> wrote in message news:0BA87358-64E0-4DA1-88D1-E171E5FC0BC6@microsoft.com...
    > this is cool, keith.
    > So, would the code be something liked...
    > $FileName=OneOfUserSpecifiedFileName
    > $regex.Matches($text) | {$_.Value.Trim() > "$FileName.txt"}
    > ???


    Needs to be a bit more like this:

    # Read file contents - this is created as array of strings
    $text = get-content c:\temp\data.txt

    # Join the array of strings into a single string, necessary
    # because we want to use a regex that spans lines
    $text = [string]::join("`n", $text)

    # Create a regex that uses singleline (. matches all) and
    # multiline (enhanced anchor mode) modes and searches for
    # text between ^ADD and another ^ADD (or end of string).
    $regex = [regex]'(?sm)^ADD.*?(?=(^ADD|\Z))'

    # Use the System.Text.RegularExpressions.Regex.Matches static method
    $regex.Matches($text) | foreach {
    $record = $_.Value.Trim();
    if ($record -match '^ADD.*?RESERVE\s+(?<Filename>\w+)') {
    $filename = $matches.Filename + ".txt"
    $record > $filename
    }
    else {
    throw "Malformed record - can't find filename - $record"
    }
    }

    --
    Keith

      My System SpecsSystem Spec

  8. #8


    light_wt Guest

    Re: are there tutorial on string processing with power-shell?

    wow! i wish i am half as smart.

    how will i remove blocks of comment in the file? the style of comment use
    /*
    block of text
    */



      My System SpecsSystem Spec

  9. #9


    Keith Hill [MVP] Guest

    Re: are there tutorial on string processing with power-shell?

    "light_wt" <lightwt@discussions.microsoft.com> wrote in message
    news:2D31F5C5-C877-4C4D-A6FE-828F21294048@microsoft.com...
    > wow! i wish i am half as smart.
    >
    > how will i remove blocks of comment in the file? the style of comment use
    > /*
    > block of text
    > */


    Use something like this:

    $text -replace '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/', ''

    --
    Keith



      My System SpecsSystem Spec

  10. #10


    light_wt Guest

    Re: are there tutorial on string processing with power-shell?

    ding, ding, ding, ding, ding!

    you're the man. keith. can i buy you a beer, man?

    can you tell me something, else? if i want to ram up on regex, so, i don't
    have to post newbie questions like this. where can i learn it on my own?

    thanks, keith.



    "Keith Hill [MVP]" wrote:

    > "light_wt" <lightwt@discussions.microsoft.com> wrote in message
    > news:2D31F5C5-C877-4C4D-A6FE-828F21294048@microsoft.com...
    > > wow! i wish i am half as smart.
    > >
    > > how will i remove blocks of comment in the file? the style of comment use
    > > /*
    > > block of text
    > > */

    >
    > Use something like this:
    >
    > $text -replace '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/', ''
    >
    > --
    > Keith
    >
    >
    >


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
are there tutorial on string processing with power-shell? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
More processing power Aylen1957 General Discussion 6 17 Nov 2008
Converting Get-ChildItems to string for processing. rush PowerShell 3 01 Feb 2008
end of line while processing a string token issue hectoritnt PowerShell 6 28 Sep 2007
Executing Power Shell Scripts from Windows Shell Mugunth PowerShell 3 02 May 2007
Encountered end of line while processing a string token Marco Shaw PowerShell 5 15 Nov 2006