delete rows in a csv file contraining specific words

Prabhasb 1 Reputation point
2021-01-28T13:34:11.057+00:00

Hi,

I want to remove the lines that contains specific words in a csv file... below is the line i written for it. but it is not working as expected, what is the mistake i am doing here...

(Get-Content -Path 'C:\searchs\DotNetresult.csv') | Where-Object { $_ -notmatch '*\v4.8*' -and $_ -notmatch 'targetFramework="net48"' } | Set-Content -Path 'C:\searchs\restults2.csv'

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Titan 206 Reputation points
    2021-01-28T17:22:08.463+00:00

    Hello @Prabhasb !

    Your Regular Expression pattern is wrong, the * is a quantifier and not a placeholder. Your pattern should throw an error, because the first * doesn't quantify anything. The second * quantifies the 8 and means, that character can occur zero or more times. The backslash and . signs do have also special meanings. Visit the provided Link for a deeper insight.

    You could try the following patterns \\v4\.8 and targetFramework="net48". Every line with the occurence of \v4.8 or targetFramework="net48" will be not transferred.

    Best whishes

    PS: And you need the automatic variable $_, only $ should throw an error, too.

    ---
    If this answer was helpful, accept and upvote it please.


  2. Anonymous
    2021-01-29T06:01:59.23+00:00

    Hi,

    You can use -notlike instead of -notmatch. The -notmatch operator matches regex pattern, not wildcard pattern.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.1#long-description

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.