Select-string problem

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5400

    by Zushakon at 2013-01-04 05:54:57

    Hi,

    I wrote a command like this one:

    Get-Childitem "\\$serwer\d$\folder\" -filter file.log -Recurse | gc | Select-String -Pattern "word" -list

    Now i thought that parametr "-list" gives only first accurence of the word "word" in every file "file.log" but.... it does not. I get all the accurence of word "word" in all files "file.log" in my folder where i only need one.

    Any help ?

    by RichardSiddaway at 2013-01-04 06:27:45

    Its because you are using Get-Content to pipe each line of the file to select-string. Each line is seen as a different object so -List is working properly

    You can do all of this in select-string

    Select-String -Path c:\reports\log*.txt -Pattern "System" -SimpleMatch -List

    only shows the first occurrence of the word System in the files

    by Zushakon at 2013-01-04 06:39:33

    [quote="RichardSiddaway"]Its because you are using Get-Content to pipe each line of the file to select-string. Each line is seen as a different object so -List is working properly

    You can do all of this in select-string

    Select-String -Path c:\reports\log*.txt -Pattern "System" -SimpleMatch -List

    only shows the first occurrence of the word System in the files[/quote]

    Big Thank You, works like a charm and thank You for explaining why my wasn't working correctly.

You must be logged in to reply to this topic.