简体   繁体   English

使用 Powershell 从包含特定单词的 CSV 中删除一行?

[英]Using Powershell to strip a row from a CSV that contains a specific word?

Here are some sample rows from my CSV file.以下是我的 CSV 文件中的一些示例行。

"ComputerName","Name","Publisher","InstallDate","EstimatedSize","Version","Wow6432Node"
"pcnumber1","Service Pack 1 for Microsoft Office 2013 (KB2850036) 32-Bit Edition","Microsoft P",,"0",,"True"
"pcnumber1","CCleaner","Piriform",,"0","5.01",

The original script pulls a large amount of information about installed software from a specified computer and outputs it into a CSV format.原始脚本从指定的计算机中提取有关已安装软件的大量信息,并将其输出为 CSV 格式。 I want to take that CSV file and remove all rows that have "Microsoft P" in them.我想获取该 CSV 文件并删除所有包含“Microsoft P”的行。 I have found how to remove specific words from a string, how to find and replace, etc. I am having a hard time finding this specific function though.我已经找到了如何从字符串中删除特定单词、如何查找和替换等。但是我很难找到这个特定的函数。

您可以使用 Where-Object 导入、选择所有不包含“Microsoft P”的元素,然后导出:

Import-Csv test.csv | where {$_.Publisher -ne "Microsoft P"} | Export-Csv New.csv -notypeinfo

You can use FINDSTR as Mark Setchell suggests.您可以按照 Mark FINDSTR建议使用FINDSTR Just indicate that it is a literal search string with /C .只需指出它是带有/C的文字搜索字符串。

FINDSTR /V /C:"Microsoft P" SomeFile.CSV > NewFile.CSV

Or if you want a PowerShell solution you can ignore the fact that it's a CSV and just treat it like a text file.或者,如果您需要 PowerShell 解决方案,您可以忽略它是 CSV 的事实,而将其视为文本文件。 Then you could:那么你可以:

Get-Content C:\Path\To\File.csv | Where{$_ -notmatch "Microsoft P"} | Out-File C:\Path\To\NewFile.csv

您可以使用多个字符串

FINDSTR /V /C:"Microsoft P" /C:"Linux R" /C:"Whatever W" SomeFile.CSV > NewFile.CSV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM