Some Idea of substrings

This topic contains 9 replies, has 7 voices, and was last updated by  random commandline 3 days, 21 hours ago.

  • Author
    Posts
  • #84197

    Rodrigo
    Participant

    Hi people,

    I would like to know if have some way to use the substrings for to get a dynamically text: I have the below result:

    $strcli = 1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "NAME012345678"

    I use the follow:

    $strcli = $strcli.Substring(50,12)

    Result:

    $strcli
    
    NAME012345678

    That works fine, but if the strings hav a name with less characters, the substring does not work.

    Someone have an idea?

  • #84199

    postanote
    Participant

    Substring are static looks. If you want dynamic, look to using RegEx string match.

    See details here:

    PowerShell: Working With Regular Expressions (regex)
    'social.technet.microsoft.com/wiki/contents/articles/4310.powershell-working-with-regular-expressions-regex.aspx'

    Powershell regular expressions
    'powershelladmin.com/wiki/Powershell_regular_expressions'

    PowerShell regex crash course – Part 2 of 5
    'blogs.technet.microsoft.com/heyscriptingguy/2016/10/07/powershell-regex-crash-course-part-2-of-5'

    PowerShell Problem Solver: PowerShell String Parsing with Regular Expressions
    'petri.com/powershell-string-parsing-with-regular-expressions'

  • #84202

    Will Prather
    Participant

    Strings are hard, because they aren't ALWAYS in the same format, BUT if they are, you can do some things. If you're final string is always inside double quotes, the below works.

    $strcli = 1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "NAME012345678"
    $substring = $strcli.split("'")[1]
    

    If it doesn't always have double quotes, but does have ":", then you could do something like,

    $strcli.split(':')[-1].trim().trim('"')
    
  • #84223

    Richard Siddaway
    Moderator

    You may get away with

    PS> $strcli.split('G: "')[6]
    NAME012345678

  • #84226

    Rodrigo
    Participant

    Hi,

    Thank you for all answer. Let me explain a more about the script. My need is get some information for an equipment with SNMPWalk. When I run the command (walk), the result is an array. See below:
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "54321C013217A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 = STRING: "12345F100417A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: "54321C012817A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777473 = STRING: "12345F100317A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777728 = STRING: "12345F100017A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777729 = STRING: "12345F100617A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777730 = STRING: "12345F1TESTE2"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777985 = STRING: "12345F100717A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777986 = STRING: "12345F10TESTE"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778241 = STRING: "12345F100817A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778497 = STRING: "12345F100917A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778753 = STRING: "12345F101017A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779009 = STRING: "12345F101117A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779265 = STRING: "12345F101217A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779521 = STRING: "12345F101317A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779777 = STRING: "12345F102117A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"

    I would like to get only value between "".

    The split almost work, but it get only first line.

    TKS,

    rc.

    • #84236

      Derf User
      Participant

      try this:

      $string = '.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"'
      $pattern = '^.*"(?(.*))"$'
      if ($string -match $pattern) {
         Write-Host $matches.name
      }
    • #84238

      Derf User
      Participant

      I was unable to edit the above reply, but try this:

      $string = '.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"'
      $pattern = '^.*"(?(.*))"$'   #Your editor is removing the less than name greater than part of this which should be directly after the ?.
      if ($string -match $pattern) {
         Write-Host $matches.name
      }

      Due to a quirk in your HTML editor I cannot write the above correctly. This is what goes directly after the ? LESS THAN name GREATER THAN (no spaces).

  • #84373

    Rodrigo
    Participant

    Thank you guys for the all answers.

    I don't have solved that issue yet. The most problem is that the result (string between "") has the various size. Some have 8 characters, and some other have plus or minus.

    thanks again.

    rc.

  • #84377

    nohwnd
    Participant

    That's pretty easy, assuming your data come as separate lines (that is why I am doing the -split on it), all you need to do is replace everything that is not in the last quotes:

    $s = @'
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "54321C013217A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 = STRING: "12345F100417A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: "54321C012817A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777473 = STRING: "12345F100317A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777728 = STRING: "12345F100017A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777729 = STRING: "12345F100617A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777730 = STRING: "12345F1TESTE2"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777985 = STRING: "12345F100717A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16777986 = STRING: "12345dF10TESTE"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778241 = STRING: "12345F100817A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778497 = STRING: "12345Fdd100917A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16778753 = STRING: "12345F101017A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779009 = STRING: "12345Fdd101117A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779265 = STRING: "12345F101217A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779521 = STRING: "12345F101317A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16779777 = STRING: "12345F102117A"
    .1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"
    '@ -split "`n" #< - making it behave like the data would come from Get-Content
    
    $s -replace '.*\"(.*?)\"\s*$','$1'
    
  • #84400

    random commandline
    Participant

    I put your example in a text file. This will output all strings between "".

    $file = Get-ChildItem .\string.txt
    
    switch -Regex -File $file {
    '"(.*)"'{$Matches[1]}
    }
    

You must be logged in to reply to this topic.