Search word – Powershell

Welcome Forums General PowerShell Q&A Search word – Powershell

Viewing 17 reply threads
  • Author
    Posts
    • #214107
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Hello, I need help.

      I have text below where and need to get only the word  “Completed
      Does anyone have a tip?

      SessionID: 2020/03 / 31-2
      Backup Specification: JOB_BACKUP
      Session type: Backup (trans)
      Started: Tuesday, March 31, 2020, 12:15:06 AM
      Finished: Tuesday, March 31, 2020, 12:15:50 AM
      Status: Completed
      Number of warnings: 4
      Number of errors: 0
      User: HI
      Group: EMPLOYED
      Host: localhost

    • #214116
      Participant
      Topics: 23
      Replies: 48
      Points: 285
      Helping Hand
      Rank: Contributor

      It depends on what you want to do with the keyword. For just picking the word, you can use match operator. If you want to validate that the string contains the keyword, you can use Like operator.

      $content = get-content filecontainthestring

      $content -match "completed"

      You will get the keyword inside $Matches variable.

      $content -like "*completed*"

      You will get output as True.

      HTH

      Roy.

    • #214125
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      It depends on what you want to do with the keyword. …

      … and it depends where this text comes from …

      You will get the keyword inside $Matches variable.

      … hmmm … sorry, that’s wrong. Have you tried it? When you use a comparison operator like a filter on more than one object you get the resulting object. In this case the line containing the match.

      You will get output as True. …

      … hhhmmmm … no again … like I mentioned above. You will only get the line containing the match. 😉

    • #214134
      Participant
      Topics: 23
      Replies: 48
      Points: 285
      Helping Hand
      Rank: Contributor

      … hmmm … sorry, that’s wrong. Have you tried it? When you use a comparison operator like a filter on more than one object you get the resulting object. In this case, the line containing the match.

      Not tried Olaf, But I have used the same in one of my scripts using foreach, and that worked as expected. Something like the following.

      $Content| % {$_ -match "completed"|out-null} 
      
      $Matches
      Name Value
      ---- -----
      0 Completed
      
    • #214140
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Not tried Olaf, But I have used the same in one of my scripts using foreach, and that worked as expected.

      Using Foreach-Object is a completely different approach and works as expected then. But you showed something else in your first reply. 😉

      I’d recommend to wait for Maicon to elaborate what he actually want to achieve. 😉

    • #214143
      Participant
      Topics: 23
      Replies: 48
      Points: 285
      Helping Hand
      Rank: Contributor

      Using Foreach-Object is a completely different approach and works as expected then. But you showed something else in your first reply

      Agree, based on that experience, I have replied my first post. 🙂

    • #216087
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Good night, sorry for the delay in responding. I forgot to comment, only the word “Completed” appears and nothing else and I have little knowledge in powershell. I need to use Foreach because it has several lines with the same result.

      • This reply was modified 1 month, 2 weeks ago by robisonaz51.
    • #216102
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Are you wanting to count how many times completed appears in the content?

    • #216105
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      No, just show the result.

    • #216120
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      I have text below where and need to get only the word  “Completed”

      Based on what you said you need to only capture the word completed. If I understand this correctly, if you have a document with the word completed in it 8 times, you want to end up with a list of 8 instances of the word Completed?

    • #216123
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Yes

    • #216141
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Ok if we assume the line always contains “Status:” before it then the following should work.

      Get-Content C:\Temp\Content.txt | foreach{
          if($_ -like 'Status:*'){
              $result = $_.split(':')[1].trim()
              $result
          }
      }
      
      
      

      If we know it’s always going to be the 6th line, we could do something like this

      Get-Content C:\Temp\Content.txt | select -Skip 5 -First 1 | foreach{
          $result = $_.split(':')[1].trim()
      }
      $result
      
      

       

      In both examples, we are simply picking out the line that has Status: in it, splitting that line at the colon, grabbing the second [1] element, and trimming the extra white space in front of the word Completed. The output is captured in the variable result and is a single string ‘Completed’

      I hope this helps!

      • This reply was modified 1 month, 2 weeks ago by Doug Maurer. Reason: Code wasn't formatted like i entered it
      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
    • #216264
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Hi @Maurer, thanks for your support.
      Script one, result appears blank.

      Script two, only one result appears

      PS C:\dp> C:\dp\Untitled2.ps1
      Completed

      PS C:\dp>

      Example Content.txt
      SessionID : 2020/03/31-2
      Backup Specification: JOByyyyy
      Session type : Backup (trans)
      Started : terça-feira, 31 de março de 2020, 00:15:06
      Finished : terça-feira, 31 de março de 2020, 00:15:50
      Status : Completed
      Number of warnings : 4
      Number of errors : 0
      User : DATAPROTECTOR
      Group : yyyyy
      Host : yyyyy

      SessionID : 2020/03/31-3
      Backup Specification: JOBXXXX
      Session type : Backup (trans)
      Started : terça-feira, 31 de março de 2020, 01:15:07
      Finished : terça-feira, 31 de março de 2020, 01:15:51
      Status : Completed
      Number of warnings : 4
      Number of errors : 0
      User : DATAPROTECTOR
      Group : XXXX
      Host : XXXXX

    • #216270
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Script one, tested it again and it appeared like script two

    • #216312
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP
      Select-String -Path 'C:\Temp\Content.txt' -Pattern 'Completed' -AllMatches | 
          Select-Object -ExpandProperty Matches | 
              Select-Object -Property Value

      … or if you just like to get the naked strings:

      (Select-String -Path 'C:\Temp\Content.txt' -Pattern 'Completed' -AllMatches).Matches.Value
    • #216396
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      I see I moved the output of $result outside of the entire foreach loop. I’ve corrected and tested it, however, I much prefer Olaf’s answer. Give them both a try and let us know if it is what you needed.

    • #221934
      Participant
      Topics: 2
      Replies: 7
      Points: 35
      Rank: Member

      Thanks Doug Maurer, is work

      Get-Content C:\Temp\Content.txt | foreach{
      if($_ -like ‘Status:*’){
      $result = $_.split(‘:’)[1].trim()
      $result
      }
      }

    • #221946
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Fantastic. You are welcome.

Viewing 17 reply threads
  • You must be logged in to reply to this topic.