Welcome Forums General PowerShell Q&A select-string clarification

  • This topic has 2 replies, 3 voices, and was last updated 6 months ago by
    js
    Participant
    .
Viewing 2 reply threads
  • 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: 2
      Replies: 1758
      Points: 3,646
      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: 28
      Replies: 752
      Points: 2,103
      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 $_
        }
      }
      
Viewing 2 reply threads
  • The topic ‘select-string clarification’ is closed to new replies.