select-string clarification

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 :slight_smile:

thanks for reading me

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.

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 $_
  }
}