select-string clarification

Welcome Forums General PowerShell Q&A select-string clarification

This topic contains 2 replies, has 3 voices, and was last updated by

js
 
Participant
4 weeks ago.

  • Author
    Posts
  • #172615

    Participant
    Topics: 1
    Replies: 0
    Points: -8
    Rank: Member

    Hi All

    im just looking for some confirmation on how the select-string command works and also on the logic behind piping data along the pipeline

    im running the following command:

    select-string -pattern 'violation of primary key' -path c:\temp\psproject\*.* | out-file -filepath c:\temp\psproject\output.txt

    my output.txt returns the data i want but my end goal is to delete that data from the log files in c:\temp\psproject

    my question is as follows, can i pipe my results from my first cmdlet to the pipeline and then have a second cmdlet delete those results from the source file( a bit like a textpad editor would do)

    im not even sure a cmdlet exists that would remove that data like i wanted to do but i was just looking for confirmation that this would be possible to do or not without the use of variables(still a beginner in powershell 🙂

    thanks for reading me

  • #172630

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    my question is as follows, can i pipe my results from my first cmdlet to the pipeline and then have a second cmdlet delete those results from the source file( a bit like a textpad editor would do)

    AFAIK there's no single cmdlet able to do what you're after. And from a programming perspective you could use an approach where you search in files for lines not containing your pattern and pipe all this to be the new content of the file.

  • #172670
    js

    Participant
    Topics: 25
    Replies: 678
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    Hmm, something like this.

    ls c:\temp\psproject | get-content | foreach {
      $filename = convert-path $_.pspath
      $newfile = $filename + '.new'
      if ($_ -match 'violation of primary key') {
        add-content output.txt "$filename,$_"
      } else { 
        add-content $newfile $_
      }
    }
    

You must be logged in to reply to this topic.