Windows Vista Forums

Question on select-string

  1. #1


    Steven Guest

    Question on select-string

    I am writing a script that needs to be able to search through a text
    file and search for the below string:

    "var INT_NMAX_BODY = 30"

    I want to change the value of this setting from 30 to 50 using -replace,
    but I'm having a hard time getting select-string to match this string.

    So I've started with something simple like:

    select-string "var INT_NMAX_BODY = 30" localization.js



    Which fails, but if I just search for "var INT_NMAX_BODY" it matches, so
    the spaces appear to be the problem.

    In the end what I'm trying to do is this:

    $content = get-content -path localizatin.js | foreach {$_ -replace "var
    INT_NMAX_BODY = 30", "var INT_NMAX_BODY = 50"}

    I'm using select-string to test my matching first and it's not working
    as I'd hoped. Could someone point me into the right direction?

    Thanks for any input or suggestions,
    Steven

      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: Question on select-string


    I don't have your file but I hope this example can give you a start:

    PS C:\Scripts>$text = "some additional text var INT_NMAX_BODY = 30
    some additional text "
    PS C:\Scripts>[regex]::Replace($text,"var INT_NMAX_BODY\s+= (30)","50")

    some additional text var INT_NMAX_BODY = 50 some additional text



    Shay
    http://scriptolog.blogspot.com



    > I am writing a script that needs to be able to search through a text
    > file and search for the below string:
    >
    > "var INT_NMAX_BODY = 30"
    >
    > I want to change the value of this setting from 30 to 50 using
    > -replace, but I'm having a hard time getting select-string to match
    > this string.
    >
    > So I've started with something simple like:
    >
    > select-string "var INT_NMAX_BODY = 30" localization.js
    >
    > Which fails, but if I just search for "var INT_NMAX_BODY" it matches,
    > so the spaces appear to be the problem.
    >
    > In the end what I'm trying to do is this:
    >
    > $content = get-content -path localizatin.js | foreach {$_ -replace
    > "var INT_NMAX_BODY = 30", "var INT_NMAX_BODY = 50"}
    >
    > I'm using select-string to test my matching first and it's not working
    > as I'd hoped. Could someone point me into the right direction?
    >
    > Thanks for any input or suggestions,
    > Steven


      My System SpecsSystem Spec

  3. #3


    Kiron Guest

    Re: Question on select-string

    # to find the string in the file

    select-string 'var INT_NMAX_BODY = 30' localizatin.js
    select-string 'var INT_NMAX_BODY {9}= 30' localizatin.js
    select-string 'var INT_NMAX_BODY += 30' localizatin.js

    # replace 'var INT_NMAX_BODY = 30' by capturing two groups
    # and placing the first one '$1' back plus the new value separated by a space
    # foreach is not necessary, if you get all the contents at once by
    # wraping the command in an expression, -replace still process each line

    $content = (get-content -path localizatin.js) -replace '(var INT_NMAX_BODY =)( 30)', '$1 50'
    $content = (get-content -path localizatin.js) -replace '(var INT_NMAX_BODY {9}=)( 30)', '$1 50'
    $content = (get-content -path localizatin.js) -replace '(var INT_NMAX_BODY +=)( 30)', '$1 50'

    --
    Kiron

      My System SpecsSystem Spec

  4. #4


    Steven Guest

    Re: Question on select-string

    Thanks both Shay and Kiron, I used a combination of what both of you
    suggested to get what I was looking for. Thanks a bunch!

    $cont = (get-content -path localization.js) -replace 'var
    INT_NMAX_BODY\s+= 30','var INT_NMAX_BODY = 50'

    Steven wrote:

    > I am writing a script that needs to be able to search through a text
    > file and search for the below string:
    >
    > "var INT_NMAX_BODY = 30"
    >
    > I want to change the value of this setting from 30 to 50 using -replace,
    > but I'm having a hard time getting select-string to match this string.
    >
    > So I've started with something simple like:
    >
    > select-string "var INT_NMAX_BODY = 30" localization.js
    >
    > Which fails, but if I just search for "var INT_NMAX_BODY" it matches, so
    > the spaces appear to be the problem.
    >
    > In the end what I'm trying to do is this:
    >
    > $content = get-content -path localizatin.js | foreach {$_ -replace "var
    > INT_NMAX_BODY = 30", "var INT_NMAX_BODY = 50"}
    >
    > I'm using select-string to test my matching first and it's not working
    > as I'd hoped. Could someone point me into the right direction?
    >
    > Thanks for any input or suggestions,
    > Steven

      My System SpecsSystem Spec

  5. #5


    Flowering Weeds Guest

    Re: Question on select-string


    "Steven"

    > I am writing a script that needs to be
    > able to search through a text
    > file and search for the below string:
    >
    > "var INT_NMAX_BODY = 30"
    >
    > I want to change the value of this
    > setting from 30 to 50
    FYI

    Another way is Microsoft's data parser
    (with a built-in graph / chart maker that
    uses the Microsoft ChartSpace objects)!

    PS> $text = "Start of text file`n"
    PS> $text += "var INT_NMAX_BODY = 30`n"
    PS> $text += "End of text file."
    PS> $text
    Start of text file
    var INT_NMAX_BODY = 30
    End of text file.
    PS> $newText = $text | LogParser.exe "SELECT
    REPLACE_STR(Text,
    'var INT_NMAX_BODY = 30',
    'var INT_NMAX_BODY = 50') FROM
    STDIN" -i:textline -headersff -statsff
    PS> $newText
    Start of text file
    var INT_NMAX_BODY = 50
    End of text file.
    PS>

    Sometimes a Log Parser one liner
    at the command line will replace text
    in many files without any script usage
    at all too!

    Yep Microsoft's Log Parser 2.2
    is Microsoft's data parsing tool.




      My System SpecsSystem Spec

Question on select-string

Similar Threads
Thread Thread Starter Forum Replies Last Post
Select-String -exclude Paul Farrimond PowerShell 9 13 Oct 2009
Re: Select-String output Jeff PowerShell 0 04 Sep 2008
problems with $var | select-string -pattern $string -q Ben Christian PowerShell 3 08 Feb 2008
select-string and .split alicain PowerShell 3 12 Oct 2007
select-string question JW PowerShell 6 24 Oct 2006