locked
finding multiple strings in a text file and deleting the entire line RRS feed

  • Question

  • Get-Content c:\output-Copy.txt | Where-Object {$_ -notmatch 'something1', 'something2', 'something3', 'something4' } | Set-Content temp.txt

    Above is the program that when it has 1 argument in the -notmatch part will find the specific string and delete the entire line. I need to be able to use multiple strings for my search, I am totally drawing a blank on this and I know its something REALLY simple but my brain is fried. The above code will work IF i only leave 'something1' and delete the rest. I need to find MULTIPLE matches and remove them all at once, what is a good way to accomplish this.

    Friday, January 9, 2015 12:51 AM

Answers

  • I opted for this, not pretty but it works :\ thanks though.

    Get-Content c:\output-Copy.txt | Where-Object {$_ -notmatch 'something1' -and $_ -notmatch 'something2' -and $_ -notmatch 'something3' -and $_ -notmatch 'something4' } | Set-Content temp.txt
    

    Friday, January 9, 2015 1:12 AM

All replies

  • You can use an alternating regex, like this:

    Get-Content c:\output-Copy.txt |
     Where-Object {$_ -notmatch 'something1|something2|something3|something4' } |
     Set-Content temp.txt


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Proposed as answer by jrv Friday, January 9, 2015 1:43 AM
    Friday, January 9, 2015 1:04 AM
  • I opted for this, not pretty but it works :\ thanks though.

    Get-Content c:\output-Copy.txt | Where-Object {$_ -notmatch 'something1' -and $_ -notmatch 'something2' -and $_ -notmatch 'something3' -and $_ -notmatch 'something4' } | Set-Content temp.txt
    

    Friday, January 9, 2015 1:12 AM
  • Shorter version of regex would be like this.

    Get-Content c:\output-Copy.txt | Where-Object {$_ -notmatch 'something[1-4]' } | Set-Content temp.txt


    Friday, January 9, 2015 2:14 AM