Extract a substring using PowerShell

前端 未结 8 978
灰色年华
灰色年华 2020-12-07 22:43

How can I extract a substring using PowerShell?

I have this string ...

\"-----start-------Hello World------end-------\"

I have to e

相关标签:
8条回答
  • 2020-12-07 22:53

    The Substring method provides us a way to extract a particular string from the original string based on a starting position and length. If only one argument is provided, it is taken to be the starting position, and the remainder of the string is outputted.

    PS > "test_string".Substring(0,4)
    Test
    PS > "test_string".Substring(4)
    _stringPS >
    

    link text

    But this is easier...

     $s = 'Hello World is in here Hello World!'
     $p = 'Hello World'
     $s -match $p
    

    And finally, to recurse through a directory selecting only the .txt files and searching for occurrence of "Hello World":

    dir -rec -filter *.txt | Select-String 'Hello World'
    
    0 讨论(0)
  • 2020-12-07 22:56

    The -match operator tests a regex, combine it with the magic variable $matches to get your result

    PS C:\> $x = "----start----Hello World----end----"
    PS C:\> $x -match "----start----(?<content>.*)----end----"
    True
    PS C:\> $matches['content']
    Hello World
    

    Whenever in doubt about regex-y things, check out this site: http://www.regular-expressions.info

    0 讨论(0)
  • 2020-12-07 22:57

    I needed to extract a few lines in a log file and this post was helpful in solving my issue, so i thought of adding it here. If someone needs to extract muliple lines, you can use the script to get the index of the a word matching that string (i'm searching for "Root") and extract content in all lines.

    $File_content = Get-Content "Path of the text file"
    $result = @()
    
    foreach ($val in $File_content){
        $Index_No = $val.IndexOf("Root")
        $result += $val.substring($Index_No)
    }
    
    $result | Select-Object -Unique
    

    Cheers..!

    0 讨论(0)
  • 2020-12-07 23:04
    PS> $a = "-----start-------Hello World------end-------"
    PS> $a.substring(17, 11)
             or
    PS> $a.Substring($a.IndexOf('H'), 11)
    

    $a.Substring(argument1, argument2) --> Here argument1 = Starting position of the desired alphabet and argument2 = Length of the substring you want as output.

    Here 17 is the index of the alphabet 'H' and since we want to Print till Hello World, we provide 11 as the second argument

    0 讨论(0)
  • 2020-12-07 23:05

    Building on Matt's answer, here's one that searches across newlines and is easy to modify for your own use

    $String="----start----`nHello World`n----end----"
    $SearchStart="----start----`n" #Will not be included in results
    $SearchEnd="`n----end----" #Will not be included in results
    $String -match "(?s)$SearchStart(?<content>.*)$SearchEnd"
    $result=$matches['content']
    $result
    

    --

    NOTE: if you want to run this against a file keep in mind Get-Content returns an array not a single string. You can work around this by doing the following:

    $String=[string]::join("`n", (Get-Content $Filename))
    
    0 讨论(0)
  • 2020-12-07 23:05

    other solution

    $template="-----start-------{Value:This is a test 123}------end-------"
    $text="-----start-------Hello World------end-------"
    
    $text | ConvertFrom-String -TemplateContent $template
    
    0 讨论(0)
提交回复
热议问题