none
Select-String - take only the following text RRS feed

  • Question

  • How get from, including, the searched word and next text?

    I use

    Get-Content 'C:\Teste\Fruit.txt'|Select-String "Naranja"

    Result

    Limones    30 Kg     Manga 50Kg    Naranja   10KG

    How to object the result is:

    Naranja   10KG

    Friday, July 3, 2020 7:46 PM

Answers

  • Get-Content 'C:\Teste\Fruit.txt' | Foreach { if ($_ -match 'Naranja.+$'){$Matches[0]}}

    The opinion expressed by me is not an official position of Microsoft

    Friday, July 3, 2020 8:39 PM

All replies

  • Get-Content 'C:\Teste\Fruit.txt' | Foreach { if ($_ -match 'Naranja.+$'){$Matches[0]}}

    The opinion expressed by me is not an official position of Microsoft

    Friday, July 3, 2020 8:39 PM
  • This ought to work:

    $SeachFor = "Naranja"
    get-content c:\junk\a.txt |
        ForEach-Object{
            if ($_ -match "($SeachFor)\s+(\w+)"){
                "Fruit: $($matches[1]), Weight: $($matches[2])"
            }
        }

    Are all of those item on the same line, or are they on individual lines?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, July 3, 2020 9:32 PM
  • Your code returns the entire line.

    E.g., search for "Manga" and you get not just what follows "Manga" but everything on the line.

    I think the OP's sample data may be wrong, though. I'm guessing that each of the items is on a separate line.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Friday, July 3, 2020 9:41 PM